Learn R Programming

sf (version 0.5-5)

aggregate.sf: aggregate an sf object

Description

aggregate an sf object, possibly union-ing geometries

Usage

# S3 method for sf
aggregate(x, by, FUN, ..., do_union = TRUE, simplify = TRUE,
  join = st_intersects)

Arguments

x

object of class sf

by

either a list of grouping elements, each as long as the variables in the data frame x (see aggregate), or an object of class sf or sfc, the geometries of which are used to find aggregation groups of x by using function join

FUN

function passed on to aggregate, in case ids was specified and attributes need to be grouped

...

arguments passed on to FUN

do_union

logical; should grouped geometries be unioned using st_union?

simplify

logical; see aggregate

join

logical spatial predicate function to use if by is a simple features object or geometry; see st_join

Value

an sf object with aggregated attributes and geometries; additional grouping variables having the names of names(ids) or are named Group.i for ids[[i]]; see aggregate.

Examples

Run this code
# NOT RUN {
m1 = cbind(c(0, 0, 1, 0), c(0, 1, 1, 0))
m2 = cbind(c(0, 1, 1, 0), c(0, 0, 1, 0))
pol = st_sfc(st_polygon(list(m1)), st_polygon(list(m2)))
set.seed(1985)
d = data.frame(matrix(runif(15), ncol = 3))
p = st_as_sf(x = d, coords = 1:2)
plot(pol)
plot(p, add = TRUE)
(p_ag1 = aggregate(p, pol, mean))
plot(p_ag1) # geometry same as pol
# works when x overlaps multiple objects in 'by':
p_buff = st_buffer(p, 0.2)
plot(p_buff, add = TRUE)
(p_ag2 = aggregate(p_buff, pol, mean)) # increased mean of second
# with non-matching features
m3 = cbind(c(0, 0, -0.1, 0), c(0, 0.1, 0.1, 0))
pol = st_sfc(st_polygon(list(m3)), st_polygon(list(m1)), st_polygon(list(m2)))
(p_ag3 = aggregate(p, pol, mean))
plot(p_ag3)
# }

Run the code above in your browser using DataLab