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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sun Mar 11 17:19:38 CET 2012


sp_0.9-97 is now on CRAN, and contains a subset method that should work,
compatible to subset.data.frame, for all objects deriving from class
Spatial.

In addition, the spplot points plotting order discussed in this thread:
https://stat.ethz.ch/pipermail/r-sig-geo/2012-March/014399.html
was resolved.


On 03/09/2012 09:31 PM, Lee Hachadoorian wrote:
> I have always relied on spdfThis[spdfThis$column == criteria,], and I
> was surprised to find out you could subset() a SpatialPointsDataFrame.
> When I heard that, I tested it on SpatialPolygonsDataFrame without
> checking the methods.
> 
> Thanks for clarifying, and for explicating the underlying logic. 
> 
> --Lee
> 
> On Fri, Mar 9, 2012 at 3:02 PM, Edzer Pebesma
> <edzer.pebesma at uni-muenster.de <mailto:edzer.pebesma at uni-muenster.de>>
> wrote:
> 
>     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 <http://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 <tel:%2B49%20251%208339763>
>      http://ifgi.uni-muenster.de
>     http://www.52north.org/geostatistics      e.pebesma at wwu.de
>     <mailto:e.pebesma at wwu.de>
> 
>     _______________________________________________
>     R-sig-Geo mailing list
>     R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>     https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 
> 

-- 
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