I am trying to find the number of consecutive days that a certain criteria is met for a stack of rasters. In this case I am trying to figure out if there are 10 consecutive days without rain for each 30 day period of the year.
Because the extensive amount of data I am using I need to come up with an efficient way to do this. ie trying to avoid using a loop through all raster cells.
I found one function that might make things easier but I can't figure out how to efficiently apply it to all raster cells. Here is an example of how it might be done easily. Here is a way to very quickly calculate the number of consecutive TRUEs.
z <- c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE) rle(z) max(rle(z)$lengths)>=3
Now the issue is how (if possible) to apply that to all cells efficiently. stackApply? calc? Not sure.
Here is an example raster stack to help:
set.seed(42)
require(raster)
r1 <- raster(nrows=10, ncols=10)
r2=r3=r4=r5=r1
r1[]= runif(ncell(r1))
r2[]= runif(ncell(r1))
r3[]= runif(ncell(r1))
r4[]= runif(ncell(r1))
r5[]= runif(ncell(r1))
rs=stack(r1,r2,r3,r4,r5)<.25