[R-sig-Geo] subset() for SpatialPOLYGONSDataFrame

Edzer Pebesma edzer.pebesma at uni-muenster.de
Fri Mar 9 21:02:21 CET 2012


Lee,

sp does not provide a subset method for SpatialPolygonsDataFrame objects:

> library(sp)
> methods(subset)
[1] subset.data.frame              subset.default
[3] subset.matrix                  subset.SpatialPixels*
[5] subset.SpatialPixelsDataFrame* subset.SpatialPoints*
[7] subset.SpatialPointsDataFrame*

   Non-visible functions are asterisked

this means that when you do call it, subset.default will be called as
the fall-back:

> subset.default
function (x, subset, ...)
{
    if (!is.logical(subset))
        stop("'subset' must be logical")
    x[subset & !is.na(subset)]
}

as you can see, this will lead to the expression:

somePolygons[somePolygons$someColumn == subsetCriteria]

which didn't give an error but leads to column selection rather than row
selection.

To be short: the subset method you want is not there in sp.


On 03/09/2012 08:44 PM, Lee Hachadoorian wrote:
> Following thread re: subsetting a SpatialPointsDataFrame. Poking around and
> found some unexpected behavior with a SpatialPolygonsDataFrame. The subset
> argument appears to be interpreted as select. That is:
> 
> subsetPolygons = subset(somePolygons, somePolygons$someColumn ==
> subsetCriteria)
> 
> returns all rows (all polygons), but the dataframe in the data slot only
> has columns selected by the logical vector spdfThis$someColumn ==
> "something". This of course is the behavior expected if I specified the
> select argument instead of the subset argument. I tried this explicitly
> naming the subset argument (subset = ...), rather than implicitly, and
> observed the same behavior.
> 
> Doing the same thing with a SpatialPointsDataFrame appears to work as
> expected.
> 
> Not uploading my data, but the actual code I ran, so you can scan it for
> stupid errors is:
> 
> #Using SpatialPolygonsDataFrame
> #Produces map of New York City with only Manhattan
> plot(spdfBoro[spdfBoro$boro_code == 1,])
> 
> #Produces map of New York City with five boros, every fifth column appears
> in data frame
> plot(subset(spdfBoro, spdfBoro$boro_code == 1))
> 
> #Using SpatialPointsDataFrame
> #Produces map of point locations in New York City, only those points in
> Manhattan
> plot(sndfWf1[sndfWf1$borough == "New York",])
> 
> #Produces map of point locations in New York City, only those points in
> Manhattan
> plot(subset(sndfWf1, sndfWf1$borough == "New York"))
> 
> Am I doing something wrong?
> 
> Regards,
> --Lee
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list