I'm trying to aggregate a simple features collection using a group variable that is a factor. Below is a simple example.
library(sf)
#> Warning: package 'sf' was built under R version 4.2.3
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
m1 = cbind(c(0, 0, 1, 1, 0), c(0, 1, 1,0, 0))
m2 = cbind(c(1, 2, 2, 1, 1), c(1, 1, 0,0, 1))
m3 = cbind(c(1, 1, 2, 2, 1), c(1, 2, 2,1, 1))
pol = st_sf(geometry = st_sfc(st_polygon(list(m1)), st_polygon(list(m2)), st_polygon(list(m3))))
pol$value = c(1,1,2)
pol
#> Simple feature collection with 3 features and 1 field
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: 0 ymin: 0 xmax: 2 ymax: 2
#> CRS: NA
#> geometry value
#> 1 POLYGON ((0 0, 0 1, 1 1, 1 ... 1
#> 2 POLYGON ((1 1, 2 1, 2 0, 1 ... 1
#> 3 POLYGON ((1 1, 1 2, 2 2, 2 ... 2
With aggregate, I get the spatial features merged as I want, but I also get a duplicate version of by grouping variable.
aggregate(pol, by = list(pol$value), unique, drop = TRUE)
#> Simple feature collection with 2 features and 2 fields
#> Attribute-geometry relationship: 0 constant, 1 aggregate, 1 identity
#> Geometry type: POLYGON
#> Dimension: XY
#> Bounding box: xmin: 0 ymin: 0 xmax: 2 ymax: 2
#> CRS: NA
#> Group.1 value geometry
#> 1 1 1 POLYGON ((0 1, 1 1, 2 1, 2 ...
#> 2 2 2 POLYGON ((1 1, 1 2, 2 2, 2 ...
I have tried naming the items in the list passed to the -by- argument, but this just results in duplicate variable called Value.1 instead of Group.1.
I am aware that I can use group_by and summarise to get the result I want, but I would like to avoid dplyr if possible. I could just drop the unwanted variable, but I feel that I must be missing something obvious.
Created on 2023-08-10 with reprex v2.0.2
aggregate()
should return, the additional grouping variable in value is described in?sf:::aggregate.sf()
as well as in?stats::aggregate()
. Perhapsaggregate(pol, by = list(pol$value), unique, drop = TRUE) |> subset(select = -Group.1)