Convert a list of sf objects into one sf
This is an old question but it is worth to note that the recent version of dplyr
(> 0.9) can bind rows of sf
objects (in a list or not):
single_sf <- dplyr::bind_rows(my_list)
class(single_sf)
[1] "sf" "data.frame"
Other packages also provide options that can be used for binding sf
objects (e.g.
mapedit:::combine_list_of_sf()
, sf:::rbind.sf
and data.table::rbindlist
), besides the do.call()
option mentioned above (see https://github.com/r-spatial/sf/issues/798# for a discussion and some benchmarking). But the dplyr
option also works for sf
objects with data frames containing different number of columns, somenthing that do.call()
, sf:::rbind.sf()
, and data.table::rbindlist()
cannot do, and was important for me working with a list of sf
objects with different number of columns.
df <- do.call(rbind, my_list)
> class(df)
[1] "sf" "data.frame"
It is worth noting that dplyr::bind_rows
and purrr::map_dfr
does not work with sf objects, and thus rbind
is better in this case.
I've found the fastest approach to do this is using data.table::rbindlist
and then converting back to sf
, as follows:
library(sf)
library(data.table)
# fast row bind
sf <- rbindlist(my_list)
# back to st
sf <- st_sf(sf)