Skip to contents

expand() returns a tibble with all combinations of selected attributes. It does not return a SpatVector because newly created combinations do not have a well-defined geometry. Use complete.SpatVector() when empty geometries should be added explicitly.

Usage

# S3 method for class 'SpatVector'
expand(data, ..., .name_repair = "check_unique")

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).

.name_repair

One of "check_unique", "unique", "universal", "minimal", "unique_quiet", or "universal_quiet". See vec_as_names() for the meaning of these options.

Value

A tibble.

Methods

Implementation of the generic tidyr::expand() method.

SpatVector

The output is a tibble with attribute combinations. Geometry is not preserved because new combinations do not have a well-defined geometry.

Examples

v <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
v$grp <- rep(c("A", "B"), length.out = nrow(v))

expand(v, grp, cpro)
#> # A tibble: 18 × 2
#>    grp   cpro 
#>    <chr> <chr>
#>  1 A     05   
#>  2 A     09   
#>  3 A     24   
#>  4 A     34   
#>  5 A     37   
#>  6 A     40   
#>  7 A     42   
#>  8 A     47   
#>  9 A     49   
#> 10 B     05   
#> 11 B     09   
#> 12 B     24   
#> 13 B     34   
#> 14 B     37   
#> 15 B     40   
#> 16 B     42   
#> 17 B     47   
#> 18 B     49