[R-sig-Geo] Help on extract function

Roger Bivand Roger.Bivand at nhh.no
Sat Feb 12 16:33:35 CET 2011


On Sat, 12 Feb 2011, 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)

One of the major strengths of the raster package is that it encapsulates 
much of the lower-level detail that experienced users find in sp. So your 
main challenge is to read and understand ?extract. If your polygon(s) are 
smaller than the raster cells, you may need a different approach.

Understanding ?aggregate in base R may also help, because you do need to 
consider what to do with the multiple raster cell values that may lie 
within each polygon. Look carefully at the example at the foot of the help 
page:

library(raster)
r <- raster(ncol=36, nrow=18)
r[] <- 1:ncell(r)
cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
polys <- SpatialPolygons(list(Polygons(list(Polygon(cds1)), 1),
   Polygons(list(Polygon(cds2)), 2)))
plot(r)
plot(polys, add=TRUE)
v <- extract(r, polys)
str(v)
unlist(lapply(v, function(x) {
   if (!is.null(x)) mean(x, na.rm=TRUE) else NA
}))
v <- extract(r, polys, fun=mean, na.rm=TRUE)
v

So consider using the fun= argument to pass through an aggregation 
function. These aggregated values can next be made into a data frame, and 
polys promoted to a SpatialPolygonsDataFrame object, ready for writing as 
a shapefile.

It is important to understand that Spatial*DataFrame objects only look 
like shapefiles (or grids) to GIS-type people - to vanilla data analysts, 
they look like, smell like, and bark like data.frame objects (but have 
additional geometry things attached).

Hope this helps,

Roger

> 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! :-)
>
> 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
>

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