Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combining multiple shapefiles in R

Tags:

r

shapefile

I have a folder with about 100 point shapefiles that are locations obtained while scat sampling of an ungulate species. I would like to merge all these point shapefiles into one shapefile in R. All the point data were in .gpx format initially which I then changed to shapefiles.

I am fairly new to R,so I am very confused as on how to do it and could not find codes that merged or combined more than a few shapefiles. Any suggestions would be much appreciated. Thanks!

like image 704
V.Menon Avatar asked Nov 27 '18 19:11

V.Menon


People also ask

How do I combine shapes in R?

You can use raster::bind . That function combines the geometries and the attributes, even if the variable names do not match. You can use union , but that is intended for combining geometries of overlapping polygons. The resulting attributes are different.


2 Answers

Building on @M_Merciless .. for long lists you can use

all_schools <- do.call(rbind, shapefile_list)

Or, alternatively, the very fast:

all_schools <- sf::st_as_sf(data.table::rbindlist(x))
like image 73
bevingtona Avatar answered Sep 18 '22 00:09

bevingtona


library(sf)

list all the shapefiles within the folder location

file_list <- list.files("shapefile/folder/location", pattern = "*shp", full.names = TRUE)

read all shapefiles, store as a list

shapefile_list <- lapply(file_list, read_sf)

append the separate shapefiles, in my example there are 4 shapefiles, this can probably be improved by using a for loop or apply function for a longer list.

all_schools <- rbind(shapefile_list[[1]], shapefile_list[[2]], shapefile_list[[3]], shapefile_list[[4]])
like image 26
M_Merciless Avatar answered Sep 18 '22 00:09

M_Merciless