I'm reading several .shp
files and I want to merge (bind rows) them into one sf data frame at the end. I tried several things but the result is not sf dataframe and the geometry cells are be in a list format.
Reproducible example:
library(tidyverse)
library(sf)
# create sample sf data frames and export them to directory: /folder/
data1 <- data.frame(attr = c(1:10), lon = c(11:20), lat = c(21:30)) %>%
st_as_sf(coords = c("lon", "lat"), dim = "XY") %>% st_set_crs(4326)
data1 %>% write_sf("folder/data1.shp")
data2 <- data.frame(attr = c(11:20), lon = c(21:30), lat = c(31:40)) %>%
st_as_sf(coords = c("lon", "lat"), dim = "XY") %>% st_set_crs(4326)
data2 %>% write_sf("folder/data2.shp")
# function for reading the exported files
read_files <- function(filename){
table <- read_sf(filename)
}
# getting the exported .shp files from the same directory
file_list <- list.files("folder/", pattern = ".shp", full.names = T)
# reading
df <- map_df(file_list, read_files)
Any suggestions are appreciated.
map_df
come from? – Edzer Pebesma