[R-sig-Geo] Ordering shapefile

Roger Bivand Roger.Bivand at nhh.no
Sun Sep 28 15:19:26 CEST 2008


On Fri, 26 Sep 2008, Jason Gasper wrote:

> I am using the readShapePoly() function in the maptools package to read in 
> shape files.  I need to order an attribute in the shapefile in ascending 
> order. For example, I have three attributes: C1,C2,C3
>            C1         C2         C3
> [1,] -0.5658135  0.0374106 -1.7702093
> [2,]  0.6783104 -0.1110221 -0.7005937
> [3,] -0.2228110  0.9176706  1.7612472
>
> ordered attributes based on ascending order in C2 would look like this:
>
>            C1         C2         C3
> [1,]  0.6783104 -0.1110221 -0.7005937
> [2,] -0.5658135  0.0374106 -1.7702093
> [3,] -0.2228110  0.9176706  1.7612472
>
> The problem of course is that I am dealing with a shapefile and thus cannot 
> treat the attributes as a vector using the "order" function.  Has anyone 
> found a way to order attributes in a shapefile? The reason I need to do this 
> is that I have a bunch of 0's that I will need to eliminate after spatial 
> weights are created (using the poly2nb() and nb2WB() functions.  So I need 
> the index for spatial weighting and cannot simply pull out each attribute and 
> create a new sorted vector.

Probably using the "[" method on the SpatialPolygonsDataFrame object:

library(spdep)
example(nc.sids)
nc.sids1 <- nc.sids[order(nc.sids$SID74),]
nc.sids$SID74
nc.sids1$SID74

Construct the indexing variable first if need be to check what is going 
on.

Hope this helps,

Roger

>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

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