Skip to contents

[Experimental]

group_nest() and nest_by() create tibbles with list-columns containing SpatVector objects.

Usage

# S3 method for class 'SpatVector'
group_nest(.tbl, ..., .key = "data", keep = FALSE)

# S3 method for class 'SpatVector'
nest_by(.data, ..., .key = "data", .keep = FALSE)

Arguments

.tbl, .data

A SpatVector.

...

Grouping specification, forwarded to group_by()

.key

the name of the list column

keep, .keep

Should the grouping columns be kept in the list column.

Value

A tibble with a list-column of SpatVector objects.

terra equivalent

terra::svc().

Methods

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

SpatVector

The nested list-column contains SpatVector objects, preserving the geometries for each group.

Examples

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

group_nest(v, grp)
#> # A tibble: 2 × 2
#>   grp   data          
#>   <chr> <list>        
#> 1 A     <SpatVctr[,3]>
#> 2 B     <SpatVctr[,3]>

nest_by(v, grp)
#> # A tibble: 2 × 2
#> # Rowwise:  grp
#>   grp   data          
#>   <chr> <list>        
#> 1 A     <SpatVctr[,3]>
#> 2 B     <SpatVctr[,3]>

# Convert to a named SpatVectorCollection.
nested <- group_nest(v, grp)

sv <- pull(nested, data)
names(sv) <- pull(nested, grp)

terra::svc(sv)
#>  class       : SpatVectorCollection
#>  length      : 2
#>  geometry    : polygons (5)
#>                polygons (4)
#>  crs (first) : ETRS89-extended / LAEA Europe (EPSG:3035)
#>  names       : A, B