Get empty SpatialPolygonsDataFrame through a subset?

I am looking for a subset of SpatialPolygonsDataFrame by attribute, but I want to allow it to return an empty SpatialPolygonsDataFrame.

If we consider objects of type SpatialPolygonsDataFrame, such as data.frames, as discussed here , we should be able to work and work with empty objects.

I am interested because I want to include this in a function that can try a subset of an attribute that essentially will not select any functions.

owd <- getwd() 
setwd(system.file("shapes", package = "maptools")) 
library(maptools) 
nc90 <- readShapeSpatial("co37_d90") 
setwd(owd)

nc90@data[nc90@data$AREA>0.15,]               # returns data.frame
bigctys <- nc90[nc90@data$AREA>0.15,]         # SpatialPolygonsDataFrame 
nc90@data[nc90@data$AREA>0.25,]               # returns empty data.frame
bigestctys <- nc90[nc90@data$AREA>0.25,]      # ERROR

Is there any way to make this work? If not, is there a way to initialize an empty SpatialPolygonsDataFrame object? The future actions that I want to perform on such an object are related to plotting on an existing map, so I would like the image to be created in any case, even if it is empty.

+4
source share
1 answer

Now you can’t. This is somewhat controversial, as for objects SpatialPointsDataFrameyou can:

library(sp)
demo(meuse, ask = FALSE)
x = meuse[F,]

albeit with warnings; In addition, validObject(x)returns FALSE, so they should be banned!

, , data.frame : , .

+4

All Articles