Learn R Programming

tidyterra (version 1.3.0)

group_map.SpatVector: Apply a function to each SpatVector group

Description

[Experimental]

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 SpatVector
group_map(.data, .f, ..., .keep = FALSE)

# S3 method for SpatVector group_modify(.data, .f, ..., .keep = FALSE)

Value

  • group_map() returns a list of results from calling .f on each group.

  • group_modify() returns a SpatVector. In that case, .f must return SpatVector objects.

Arguments

.data

A grouped or ungrouped SpatVector.

.f

A function called with .x, a SpatVector containing the rows for one group, and .y, a tibble with the group keys.

...

Additional arguments passed on to .f

.keep

are the grouping variables kept in .x

Methods

Implementation of the generic dplyr::group_map() method family for SpatVector objects.

group_map() applies .f to each group and returns a list. group_modify() requires .f to return SpatVector objects and binds the results.

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.

See Also

dplyr::group_map(), dplyr::group_modify().

Other dplyr grouping methods: group_by.SpatVector(), group_data.SpatVector(), group_nest.SpatVector(), group_split.SpatVector(), group_trim.SpatVector()

Examples

Run this code
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))

group_modify(group_by(v, grp), ~ mutate(.x, key = .y$grp))

Run the code above in your browser using DataLab