Combining vector data using rbind()
or
dplyr::bind_rows()
can be very slow in {sf}
. A
faster alternative is to use data.table::rbindlist()
or
collapse::unlist2d()
. Note, make sure all objects have the
same CRS beforehand!!
library("sf")
library("data.table")
nc = read_sf(system.file("shape/nc.shp", package = "sf"))
ls = vector("list", 500)
for (i in seq_along(ls)) ls[[i]] = nc
system.time({
dt = rbindlist(ls, use.names = FALSE)
dt[["geometry"]] = st_sfc(dt[["geometry"]], recompute_bbox = TRUE)
dt = st_as_sf(dt)
})
## user system elapsed
## 0.05 0.00 0.04
dim(dt)
## [1] 50000 15
system.time({
df_sf = do.call(rbind, ls)
})
## user system elapsed
## 8.87 0.16 9.03
dim(df_sf)
## [1] 50000 15