Learn R Programming

tidyterra (version 1.3.0)

complete.SpatVector: Complete missing combinations in a SpatVector

Description

complete() turns implicit missing combinations in a SpatVector into explicit rows while preserving geometry and spatial metadata.

Usage

# S3 method for SpatVector
complete(data, ..., fill = list(), explicit = TRUE)

Value

A SpatVector object.

Arguments

data

A SpatVector.

...

<data-masking> Specification of columns to expand or complete. Columns can be atomic vectors or lists.

  • To find all unique combinations of x, y and z, including those not present in the data, supply each variable as a separate argument: expand(df, x, y, z) or complete(df, x, y, z).

  • To find only the combinations that occur in the data, use nesting: expand(df, nesting(x, y, z)).

  • You can combine the two forms. For example, expand(df, nesting(school_id, student_id), date) would produce a row for each present school-student combination for all possible dates.

When used with factors, expand() and complete() use the full set of levels, not just those that appear in the data. If you want to use only the values seen in the data, use forcats::fct_drop().

When used with continuous variables, you may need to fill in values that do not appear in the data: to do so use expressions like year = 2010:2020 or year = full_seq(year,1).

fill

A named list that for each variable supplies a single value to use instead of NA for missing combinations.

explicit

Should both implicit (newly created) and explicit (pre-existing) missing values be filled by fill? By default, this is TRUE, but if set to FALSE this will limit the fill to only implicit missing values.

Methods

Implementation of the generic tidyr::complete() method for SpatVector objects.

complete() preserves the geometry column while expanding missing combinations. New combinations receive empty geometries.

See Also

tidyr::complete().

Other tidyr verbs for handling missing values: drop_na.Spat, expand.SpatVector(), fill.SpatVector(), replace_na.Spat

Examples

Run this code
v <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
v <- dplyr::mutate(v, grp = ifelse(iso2 %in% c("ES-AV", "ES-BU"), "a", "b"))

complete(v, grp, tidyr::nesting(iso2, name)) |>
  glimpse()

Run the code above in your browser using DataLab