[R-sig-Geo] read shapefile, iterate through rows in attribute table, update attribute table

Roger Bivand Roger.Bivand at nhh.no
Fri Aug 8 18:51:06 CEST 2008


On Fri, 8 Aug 2008, Murray Richardson wrote:

> Hello users,
>
> I am using R to create some scripts that involve a combination of SAGA 
> functions (via RSAGA) and R processes. I need to read a polygon shapefile 
> into R, interate through each polygon feature in the attribute table, convert 
> it to its own shapefile, and do some processing on that individual polygon 
> shapefile in SAGA.  I will then need to update the row's attributes (in the 
> original shapefile) with the results of the SAGA process. 
> I'm primarily looking on advice on how to read and iterate through the 
> shapefile rows, converting each one into a temporary shapefile.  I also 
> appreciate advice on updating records in a shapefile attribute table. 
> Thanks in advance.

If the shapefile is read into a SpatialPolygonsDataFrame, then the [ 
subset operator works just as for other R objects. Something like:

for (i in 1:length(slot(x, "polygons"))) {
   tmpx <- x[i,]
   # do something with tmpx
}

will extract each Polygons object in turn as a (multi)-geometry with its 
vector of attributes.

Roger

>
> Murray Richardson
>
> _______________________________________________
> 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