[R-sig-Geo] Help on extract function

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sat Feb 12 14:16:03 CET 2011



On 02/12/2011 02:05 PM, Sean O'Riordain wrote:
> Hi Rahul,
> 
> First look at
>   str(polygon, max.level=2)
> to understand how a shapefile is represented in R, then have a look at
>   str(polygon at data)
> which is a dataframe - you can add a new column to this dataframe like...
>   polygon at data$newcolumn <- 1:nrow(polygon at data)
> i.e you can treat this dataframe like any other as long as you're in
> read-only mode or just adding columns... if you add rows or delete
> rows or modify ID fields then beware!!! these rows are linked to the
> actual polygons held elsewhere as arrays... don't touch for the moment
> I'd suggest! :-)

You can, if you avoid modifying object slots directly, but use the
appropriate methods. So to select the first ten polygons with their
attributes you should NEVER do something like:

polygon at data = polygon at data[1:10,] # NO!

but rather

polygonSel = polygon[1:10,]

For addressing or adding columns you can also use

polygon$existingcolumn
polygon[["existingcolumn"]]

and assigning:

polygon$newcolum  <- 1:nrow(polygon at data)

which will have the same effect as

polygon at data$newcolumn <- 1:nrow(polygon at data)

... whatever you prefer!
--
Edzer


> 
> then you can save out the shape file again...
> 
> cheers
> Sean
> Dublin
> Ireland
> 
> 
> On 12 February 2011 05:16, Rahul Raj <rahulosho at gmail.com> wrote:
>> Dear all,
>>
>> I am using the command "extract(raster, polygon)".
>>> I want to run this command in loop to extract the raster cells values by
>> polygon number in each iteration.
>>> I also want to attach one attribute column to shape file and write the
>> shape file with added attribute.
>> Please help me how can I do this in R
>>
>> Quick reply is highly appreciated and Thanks in advance
>>
>> Rahul Raj
>> Indian Institute of Remote Sensing, India.
>>
>>        [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
> 
> _______________________________________________
> R-sig-Geo mailing list
> 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