I need to compute number of three consecutive days when value of each pixel in a raster stack (x
) is above a given threshold (defined by another raster y
). I tried using rle
for the purpose with calc
as follows after stacking x
and y
together into new raster a
:
library(raster)
fn<-function(a) with(rle(a), sum(lengths>=3 & values>a[[nlayers(a)]]))
calc(b,fn)
However, I am getting the error:
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
cannot use this function
Reproducible sample:
x1 <- raster(nrows=10, ncols=10)
x2=x3=x4=x5=x6=x1
x1[]= runif(ncell(x1))
x2[]= runif(ncell(x1))
x3[]= runif(ncell(x1))
x4[]= runif(ncell(x1))
x5[]= runif(ncell(x1))
x6[]= runif(ncell(x1))
x=stack(x1,x2,x3,x4,x5,x6)
y=x1
y[]= runif(ncell(x1))
a<-stack(x,y)
Can someone please help.