[R-sig-Geo] extracting and manipulating a RasterStack

Lyndon Estes lestes at princeton.edu
Tue Aug 9 05:30:01 CEST 2011


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:

> t <- area(r)
# 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:
> I have been struggling with something seemingly straightforward for several days now, and am hoping for some assistance.
>
> I have calculated the area for cells within a raster, and now want to extract the area for specific cells and sum the area of those cells together.  I have tried a number of functions such as stackSelect, but think I have had trouble using the proper syntax in my logical statements. I am new to the raster package, however, so this may be my issue.
>
> For example, if I have data that looks like this:
>
> require(raster)
>
> r <- raster(ncol=10, nrow=10)
> r[]=1:ncell(r)
> t <- area(r)
> s <- stack(r, t)
>
> for all values in first layer of s (equivalent to the raster r) with the values 50 and 75, I want to take the corresponding values in the second layer (equivalent to t), create a new raster (call it 'z') where the remaining cells are given a value of 0 (or NA) and finally sum the values of the z.
>
> Any suggestions appreciated.
> Thank you,
> Sara
>
>
>        [[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
>



More information about the R-sig-Geo mailing list