Skip to content

subselection of Polygons

3 messages · Stéphane Dray, Roger Bivand

#
Hello list,

I have a question concerning the [ operator for SPDF. Consider the 
following example :

library(maptools)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
plot(xx, border="blue", axes=TRUE, las=1)
text(getSpPPolygonsLabptSlots(xx), labels=getSpPPolygonsIDSlots(xx), cex=0.6)
as(xx, "data.frame")[1:5, 1:6]
xxx <- xx[xx$SID74 < 2,]

The last line is equivalent to :

xxx <- xx[which(xx$SID74 < 2),]

I supposed that I can easily have the inverse selection by using '-' but 
it does not work :

xxx <- xx[which(xx$SID74 < 2),]

returns the following error message :

Erreur dans validObject(.Object) : invalid class "SpatialPolygons" 
object: length mismatch

I do not understand what is the problem (sp 0.9-8, R 2.4.0).

Thanks,
#
Sorry,
the line which does not work is :

xxx <- xx[-which(xx$SID74 < 2),]
St?phane Dray wrote:

  
    
#
On Fri, 2 Mar 2007, St?phane Dray wrote:

            
Correct. The workaround is to take the complement. Getting negative 
indices to work is on the TODO list, it is a problem in the "[" method.

xxx <- xx[!(seq(along=slot(xx, "polygons")) %in% which(xx$SID74 < 2)),]

works.

Roger