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,yandz, including those not present in the data, supply each variable as a separate argument:expand(df, x, y, z)orcomplete(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()andcomplete()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, useforcats::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:2020oryear = full_seq(year,1).- .name_repair
One of
"check_unique","unique","universal","minimal","unique_quiet", or"universal_quiet". Seevec_as_names()for the meaning of these options.
Value
A tibble.
Methods
Implementation of the generic tidyr::expand() method.
See also
tidyr::expand(), complete.SpatVector()
Other tidyr verbs for handling missing values:
complete.SpatVector(),
drop_na.Spat,
fill.SpatVector(),
replace_na.Spat
Other tidyr methods:
complete.SpatVector(),
drop_na.Spat,
fill.SpatVector(),
nest.SpatVector(),
pivot_longer.SpatVector(),
pivot_wider.SpatVector(),
replace_na.Spat,
uncount.SpatVector(),
unite.Spat
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
