Merging a List of Spatial Polygon Objects in R

polygonrrgeossp

I have a list of spatial buffers (30000 buffers) that I built with the function lapply:

buff.pts <- lapply(1:nrow(pts.prj), FUN=function(l){
  buff <- gBuffer(pts.prj[l,], width=1000) ## 1km
  return(buff)
}))

> head(buff.pts)
[[1]]
class       : SpatialPolygons 
features    : 1 
extent      : 307941.8, 311941.8, 4994518, 4998518  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0 

[[2]]
class       : SpatialPolygons 
features    : 1 
extent      : 307226, 311226, 4991153, 4995153  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0 

From this list, how can I merge all spatial buffers to obtain a shapefile with the 30000 buffers (or features) ? (This shapefile will then be used in the function aggregate to aggregate spatial polygons by attributes.)

I tested this code but I obtain this error message:

test <- as.data.frame(do.call("rbind", buff.pts))
Error in as.data.frame(do.call("rbind", buff.pts)) : 
  error in evaluating the argument 'x' in selecting a method for function 'as.data.frame': Error in validObject(res) : 
  invalid class “SpatialPolygons” object: non-unique Polygons ID slot values

Best Answer

To merge a list of Spatial objects, you can do:

library(raster)
m <- do.call(bind, buff.pts)