I have a list of SpatialPolygons I created using lapply and the raster::buffer
function. I would like to convert the files in the list back to a list of SpatialPolygonsDataFrame objects. I tried the answer provided here (although note my issue is slightly different because I would like to end up with a LIST of SpatialPolygonsDataFrame objects): R - SpatialPolygonsDataFrame from a list of SpatialPolygons, but i don't know how to get unique IDs for each object in the list. When I apply that code, I get the following error msg:
#Getting polygon IDs
IDs <- sapply(list_of_SPols, function(x)
slot(slot(x, "polygons")[[1]], "ID"))
#Checking
length(unique(IDs)) == length(list_of_SPols) #output = [1] FALSE
#Making SpatialPolygons from list of polygons
Spol <- SpatialPolygons(lapply(list_of_SPols,
function(x) slot(x, "polygons")[[1]]))
#output = Error in validObject(res) :
#invalid class “SpatialPolygons” object: non-unique Polygons ID slot
#values
My SpatialPolygons list elements have unique index values, but when I check the output of the IDs object created above, a subset of it looks like this:
#[1] "1" "1" "1" "1" "1" "1" "1"....
So all list elements have the same IDs. So I think what I need to do is create unique IDs equal to the index numbers?
How do I do this, then how do I go on to create a list of SpatialPolygonsDataFrame objects?
match.ID = FALSE
. But there is more that you do wrong, I think, but it is hard to answer without data. – Robert Hijmans