aggregation of spatial objects
aggregate.Rdspatial aggregation of thematic information in spatial objects
Arguments
- x
object deriving from Spatial, with attributes
- by
aggregation predicate; if
byis a Spatial object, the geometry by which attributes inxare aggregated; ifbyis a list, aggregation by attribute(s), see aggregate.data.frame- FUN
aggregation function, e.g. mean; see details
- ...
arguments passed on to function
FUN, unlessminDimensionis specified, which is passed on to function over- dissolve
logical; should, when aggregating based on attributes, the resulting geometries be dissolved? Note that if
xhas classSpatialPointsDataFrame, this returns an object of classSpatialMultiPointsDataFrame; deprecated- areaWeighted
logical; should the aggregation of
xbe weighted by the areas it intersects with each feature ofby? See value; deprecated.
Value
The aggregation of attribute values of x either over the
geometry of by by using over for spatial matching,
or by attribute values, using aggregation function FUN.
If areaWeighted is TRUE, FUN is ignored and the
area weighted mean is computed for numerical variables, or if all
attributes are factors, the area dominant factor level (area
mode) is returned. This computes the intersection of x
and by; see examples below. As this uses code from package
rgeos, it is deprecated as package rgeos will retire.
If by is missing, aggregates over all features.
Details
For as far as these functions use package rgeos, (lines, polygons, dissolve = TRUE), they are deprecated as rgeos will retire; try using sf::aggregate instead.
FUN should be a function that takes as first argument a
vector, and that returns a single number. The canonical examples
are mean and sum. Counting features is obtained when
summing an attribute variable that has the value 1 everywhere.
Author
Edzer Pebesma, edzer.pebesma@uni-muenster.de
Examples
data("meuse")
coordinates(meuse) <- ~x+y
data("meuse.grid")
coordinates(meuse.grid) <- ~x+y
gridded(meuse.grid) <- TRUE
i = cut(meuse.grid$dist, c(0,.25,.5,.75,1), include.lowest = TRUE)
j = sample(1:2, 3103,replace=TRUE)
x = aggregate(meuse.grid["dist"], list(i=i,j=j), mean, dissolve = FALSE)
spplot(x["j"], col.regions=bpy.colors())