Merges features that share the same values of by into a single geometry,
then (by default) splits multipart results back to single-part features.
dissolve_by(x, by, explode = TRUE)A SpatVector, or an object coercible to one via terra::vect().
Character vector of attribute names to dissolve on.
Logical. If TRUE (the default), split multipart geometries
back to single-part with terra::disagg().
A SpatVector with one (or, when explode = TRUE, one or more)
feature per combination of by values present in x, carrying only the
by columns.
This exists because terra::aggregate() cannot dissolve on several
columns when any of them contains NA: it returns a SpatVector whose
attribute table has fewer rows than it has geometries, which then errors on
the next access (nrow dataframe does not match nrow geometry). An
unclassified category is usually still a category – an unclassified seral
stage, an unmapped landbase status – so dropping or corrupting it is rarely
what a dissolve is meant to do. Here NA values are encoded to a sentinel
before aggregating and restored afterwards, so every NA group survives as a
group of its own.
The agg_n bookkeeping column terra::aggregate() adds is removed.
Other SpatVector helpers:
drop_values(),
expanse_planar(),
nn_distance()
v <- terra::vect(c(
"POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))",
"POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0))",
"POLYGON ((3 0, 4 0, 4 1, 3 1, 3 0))"
))
v$class <- c("a", "a", NA)
## the NA feature survives as its own group
as.data.frame(dissolve_by(v, "class"))
#> class
#> 1 <NA>
#> 2 a