[R-sig-Geo] Antw: [raster] formal way of addressing specific layer in RasterStack?

Matteo Mattiuzzi matteo.mattiuzzi at boku.ac.at
Thu Dec 6 00:57:12 CET 2012


Hi,


using the example of ?extract you can do:


r <- raster(ncol=36, nrow=18)
r[] <- 1:ncell(r)
r <- stack(r,r,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)))
     

v <- extract(r[[2]],polys,mean)


Matteo


>>> dcd  06.12.12 0.33 Uhr >>>
Just curious if this is the most optimal way of addressing a specific layer
in a RasterStack:

-RasterStack with 14 layers called "stack"
-polygon layer called "sites"

# extract cell values from layer 2 of "stack" RasterStack using "sites"
polygon
extract_stack_sites<-extract(stack at layers[[2]], sites))

# extract summary stats (mean) for each polygon
extract_stack_sites_mean<-unlist(lapply(extract_stack_sites, function(x) if
(!is.null(x)) mean(x, na.rm=TRUE) else NA ))

The above works, but perhaps there's a more elegant solution, especially if
dealing with a range, or subset of layers within a RasterStack?

Cheers



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/raster-formal-way-of-addressing-specific-layer-in-RasterStack-tp7581866.html
Sent from the R-sig-geo mailing list archive at Nabble.com.

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



More information about the R-sig-Geo mailing list