dplyr::group_map() and dplyr::group_modify() are purrr-style functions
that can be used to iterate on grouped SpatVector objects.
Usage
# S3 method for class 'SpatVector'
group_map(.data, .f, ..., .keep = FALSE)
# S3 method for class 'SpatVector'
group_modify(.data, .f, ..., .keep = FALSE)Value
group_map()returns a list of results from calling.fon each group.group_modify()returns aSpatVector. In that case,.fmust returnSpatVectorobjects.
Details
Each conceptual group is exposed to .f with two pieces of information:
.x, the subset of rows for the group as a SpatVector, and .y, a one-row
tibble with one column per grouping variable that identifies the group.
These methods also work on ungrouped SpatVector objects. In that case,
.f is applied to the entire object and .y is a one-row tibble with no
columns.
Methods
Implementation of the generic dplyr::group_map() family for
SpatVector objects.
SpatVector
group_map() applies .f to each group and returns a list.
group_modify() requires .f to return SpatVector objects and binds the
results.
See also
dplyr::group_map() and dplyr::group_modify().
Other dplyr grouping methods:
group_by.SpatVector(),
group_nest.SpatVector(),
group_split.SpatVector(),
group_trim.SpatVector()
Examples
v <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
v$grp <- rep(c("A", "B"), length.out = nrow(v))
group_map(group_by(v, grp), ~ nrow(.x))
#> [[1]]
#> [1] 5
#>
#> [[2]]
#> [1] 4
#>
group_modify(group_by(v, grp), ~ mutate(.x, key = .y$grp))
#> class : SpatVector
#> geometry : polygons
#> dimensions : 9, 4 (geometries, attributes)
#> extent : 2892687, 3341372, 2017622, 2361600 (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names : iso2 cpro name key
#> type : <chr> <chr> <chr> <chr>
#> values : ES-AV 05 Avila A
#> ES-LE 24 Leon A
#> ES-SA 37 Salamanca A
#> ...
