[R-sig-Geo] Count frequency in raster stacks
Thiago V. dos Santos
thi_veloso at yahoo.com.br
Wed Feb 17 10:25:02 CET 2016
Hi all,
I am trying to count the frequency of values in raster objects based on specified intervals. For example, I have a raster storing monthly rainfall totals ranging from 50 to 600 mm. And I need to know how many values fall in the interval 0-100, how many on 101-200, 201-300 and so on.
I managed to do it in a single raster:
require(raster)
## scratch a raster and fill some random values
r <- raster(nrows=22, ncols=20, xmn=-58, xmx=-48, ymn=-33, ymx=-22)
r[] <- round(runif(22 * 20, min=0, max=600), digits=0)
# Count raster cells (excluding NA's) and set intervals
ncells <- sum(!is.na(r[]))
intervals <- seq(0, 600, 100)
# Count frequencies and calculate percentage
t <- table(cut(as.vector(r), intervals, include.lowest=TRUE)) / ncells * 100
df <- data.frame(round(t, digits=2))
However, how can I do the same for a raster stack, storing the count of each layer in a column of the resulting data frame?
Thanks in advance,
-- Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
More information about the R-sig-Geo
mailing list