[R-sig-Geo] Count values "greater than" in rasterbrick layers

Stachelek, Joseph jstachel at sfwmd.gov
Wed Jul 16 14:30:45 CEST 2014


Hi Thiago,

Ignoring the fact that you have negative rainfall amounts in your example, I would go about this problem by reclassifying the raster brick to a binary classification. Then if you sum you get raw counts.

#count wet days
b.wet<-reclassify(b,c(-Inf,0,0))
b.wet<-reclassify(b.wet,c(0,+Inf,1))
plot(b.wet[[1]])
plot(sum(b.wet))

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Thiago V. dos Santos
Sent: Wednesday, July 16, 2014 5:51 AM
To: R-SIG list
Subject: [R-sig-Geo] Count values "greater than" in rasterbrick layers

Dear all,

I have a 31-layer rasterbrick where each layer represents daily rainfall records for a certain month.

My goal is to perform two calculations in this brick:

a) Sum up total rainfall for the entire month

b) For the entire month (all rasterbrick layers), calculate the number of days where it rained (i.e. rainfall > 1)

Problem a) is straightforward and I solved it with the help of a simple calc command.

Problem b), however, is where I am having troubles. The command I came up with doesn't work, as the final raster has values way greater than 31 (which would be the limit of each cell, if it had rained every day of the month on that cell).

This is my code so far. Any suggestions?

library(raster)

# generate raster data
r <- raster(ncol=384, nrow=190)
b <- brick(sapply(1:31, function(i) setValues(r, rnorm(ncell(r), i, 3))))

# sum up rain
acc.rain <- calc(b, fun=sum)

# count wet days
wet.days <- calc(b, fun=function(x,na.rm) sum(x[x>1]))


Many thanks in advance,
--
Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm
Phone: (612) 323 9898
        [[alternative HTML version deleted]]



We value your opinion. Please take a few minutes to share your comments on the service you received from the District by clicking on this link<http://my.sfwmd.gov/portal/page/portal/pg_grp_surveysystem/survey%20ext?pid=1653>.



More information about the R-sig-Geo mailing list