As Matteo mentionned, you can use extract, however using layer option
instead of brackets might be safer and more conviennent if you want to
extract multiple layers.
v <- extract(stack, sites, mean, na.rm = TRUE, layer=2, n = 1)

Etienne


2012/12/5 Matteo Mattiuzzi <matteo.mattiuzzi@boku.ac.at>

> 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@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@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

