summarise.SpatVector: Summarise each group of a 'SpatVector' down to one geometry

View source: R/summarise-SpatVector.R

summarise.SpatVectorR Documentation

Summarise each group of a SpatVector down to one geometry

Description

summarise() creates a new SpatVector. It returns one geometry for each combination of grouping variables; if there are no grouping variables, the output will have a single geometry summarising all observations in the input and combining all the geometries of the SpatVector. It will contain one column for each grouping variable and one column for each of the summary statistics that you have specified.

summarise.SpatVector() and summarize.SpatVector() are synonyms

Usage

## S3 method for class 'SpatVector'
summarise(.data, ..., .by = NULL, .groups = NULL, .dissolve = TRUE)

## S3 method for class 'SpatVector'
summarize(.data, ..., .by = NULL, .groups = NULL, .dissolve = TRUE)

Arguments

.data

A SpatVector.

...

<data-masking> Name-value pairs of summary functions. The name will be the name of the variable in the result.

The value can be:

  • A vector of length 1, e.g. min(x), n(), or sum(is.na(y)).

  • A data frame with 1 row, to add multiple columns from a single expression.

.by

<tidy-select> Optionally, a selection of columns to group by for just this operation, functioning as an alternative to group_by(). For details and examples, see ?dplyr_by.

.groups

[Experimental] Grouping structure of the result.

  • "drop_last": drops the last level of grouping. This was the only supported option before version 1.0.0.

  • "drop": All levels of grouping are dropped.

  • "keep": Same grouping structure as .data.

  • "rowwise": Each row is its own group.

When .groups is not specified, it is set to "drop_last" for a grouped data frame, and "keep" for a rowwise data frame. In addition, a message informs you of how the result will be grouped unless the result is ungrouped, the option "dplyr.summarise.inform" is set to FALSE, or when summarise() is called from a function in a package.

.dissolve

logical. Should borders between aggregated geometries be dissolved?

Value

A SpatVector.

terra equivalent

terra::aggregate()

Methods

Implementation of the generic dplyr::summarise() method.

SpatVector

Similarly to the implementation on sf this function can be used to dissolve geometries (with .dissolve = TRUE) or create MULTI versions of geometries (with .dissolve = FALSE). See Examples.

See Also

dplyr::summarise(), terra::aggregate()

Other single table verbs: arrange.SpatVector(), filter.Spat, mutate.Spat, rename.Spat, select.Spat, slice.Spat

Other dplyr verbs that operate on group of rows: count.SpatVector(), group-by.SpatVector, rowwise.SpatVector()

Other dplyr methods: arrange.SpatVector(), bind_cols.SpatVector, bind_rows.SpatVector, count.SpatVector(), distinct.SpatVector(), filter-joins.SpatVector, filter.Spat, glimpse.Spat, group-by.SpatVector, mutate-joins.SpatVector, mutate.Spat, pull.Spat, relocate.Spat, rename.Spat, rowwise.SpatVector(), select.Spat, slice.Spat

Examples

library(terra)
library(ggplot2)

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

# Grouped
gr_v <- v |>
  mutate(start_with_s = substr(name, 1, 1) == "S") |>
  group_by(start_with_s)

# Dissolving
diss <- gr_v |>
  summarise(n = dplyr::n(), mean = mean(as.double(cpro)))

diss

autoplot(diss, aes(fill = start_with_s)) +
  ggplot2::labs(title = "Dissolved")

# Not dissolving
no_diss <- gr_v |>
  summarise(n = dplyr::n(), mean = mean(as.double(cpro)), .dissolve = FALSE)

# Same statistic
no_diss

autoplot(no_diss, aes(fill = start_with_s)) +
  ggplot2::labs(title = "Not Dissolved")

tidyterra documentation built on March 11, 2026, 9:08 a.m.