I have several polygon shapefiles that I need to merge into one using a loop in R. I want to start with an empty shapefile and merge it with the existing shapefiles. All shapefiles will be chacked before the merge that they have the same fields.
I can think of two options:
- create an empty shapefile in ArcMap and read it into R. I tried this with readOGR and got an error:
library(rgdal) EI_GIS_ALL = readOGR(dsn = EI_GIS_ALL_dsn, layer=EI_GIS_ALL_FileName) Error in readOGR(dsn = EI_GIS_ALL_dsn, layer = EI_GIS_ALL_FileName) : no features found
- Create an empty shapefile in R and use union to merge it with existing shapefiles. The union is like in maycca's solution in Merging multiple SpatialPolygonDataFrames into 1 SPDF in R?
Any suggestions?
shp <- list.files(getwd(), "shp$")
then read in datas <- lapply(shp, function(x) sf::st_read(x) )
finally, merge and coerce to sps <- as(do.call(rbind, s), "Spatial")
With large data you will also get better read times.