Skip to contents

[Experimental]

This method drops unused levels of all factors that are used as grouping variables and recalculates the grouping structure.

group_trim() is particularly useful after a filter() that is intended to select a subset of groups.

Usage

# S3 method for class 'SpatVector'
group_trim(.tbl, .drop = group_by_drop_default(.tbl))

Arguments

.tbl

A SpatVector object. See Methods.

.drop

See group_by.SpatVector().

Value

A SpatVector object with updated grouping metadata.

Details

See Details on dplyr::group_trim().

Methods

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

Examples

v <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))

v$group <- rep(c("A", "B", "C"), 3)

v |>
  group_by(group) |>
  filter(group == "B", .preserve = TRUE) |>
  group_trim()
#> class       : SpatVector
#> geometry    : polygons
#> dimensions  : 3, 4  (geometries, attributes)
#> extent      : 2892687, 3296229, 2049224, 2331004  (xmin, xmax, ymin, ymax)
#> coord. ref. : ETRS89-extended / LAEA Europe (EPSG:3035)
#> names       :  iso2  cpro       name group
#> type        : <chr> <chr>      <chr> <chr>
#> values      : ES-BU    09     Burgos     B
#>               ES-SA    37  Salamanca     B
#>               ES-VA    47 Valladolid     B