[R-sig-Geo] subselection of Polygons

Roger Bivand Roger.Bivand at nhh.no
Fri Mar 2 11:38:38 CET 2007


On Fri, 2 Mar 2007, Stéphane Dray wrote:

> Sorry,
> the line which does not work is :
> 
> xxx <- xx[-which(xx$SID74 < 2),]
> 

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

> 
> 
> Stéphane Dray wrote:
> > 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,
> >
> >   
> 
> 
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list