Skip to content
Prev 12476 / 29559 Next

extracting and manipulating a RasterStack

Hi Sara,

The following is slightly different from what you are trying to do,
but should give you an area value for cells of a given value, without
having to stack your rasters:

r <- raster(ncol = 10, nrow = 10)
res(r) <- 10  # Giving it a square grid cell, which remakes raster
into 18 rows & 36 columns
r[] <- 1:ncell(r)
plot(r)
z <- ((r == 50) | (r == 75)) * 1 # Binary raster
plot(z)
# The following would give you area in hectares for the selected cells,
# assuming that your actual raster is projected in units of meters
# (e.g. UTM, Albers) unlike the dummy raster here

(sum(getValues(z)) * res(r)[1]^2) / 10000
[1] 0.02

I hope that is somewhat on the mark.

Interestingly, I get the following error when I try to use area:
# Error in b - a : 'b' is missing

Cheers, Lyndon
On Mon, Aug 8, 2011 at 6:07 PM, Sara Maxwell <smaxwell at ucsc.edu> wrote: