Vijay
Vijay

Reputation: 151

Incorrect behavior of data.table when used with sf package

data.table does incorrect aggregation when used with sf::st_union whereas dplyr seems to handle it correctly. Can someone please explain why data.table yields this result?

library(data.table)
library(sf)
library(dplyr)
nc <- st_read(system.file("shape/nc.shp",package="sf"))
nc_DT <- as.data.table(nc)
nc %>% group_by(SID74) %>% summarise(geom = st_union(geometry)) %>% nrow # prints 23 (correct answer)
nrow(nc_DT[,st_union(geometry),by=SID74])  # prints 83 (incorrect answer)

Upvotes: 2

Views: 352

Answers (1)

akrun
akrun

Reputation: 887691

If we place it in a list, the number of rows will be 23

res <- nc_DT[, .(geom = st_union(geometry)),by=SID74]
nrow(res)
#[1] 23

You can then easily recover the attributes of the geometry column like this:

res <- st_sf(res)
plot(res)

Upvotes: 4

Related Questions