title: Calculating number of three consecutive values above a threshold (in raster stack)
body: <p>I need to compute number of three consecutive days when value of each pixel in a raster stack (<code>x</code>) is above a given threshold (defined by another raster <code>y</code>). I tried using <code>rle</code> for the purpose with <code>calc</code> as follows after stacking <code>x</code> and <code>y</code> together into new raster <code>a</code>:</p>
  
  <pre><code>library(raster)    
  fn&lt;-function(a) with(rle(a), sum(lengths&gt;=3 &amp; values&gt;a[[nlayers(a)]]))
  calc(b,fn)
  </code></pre>
  
  <p>However, I am getting the error:</p>
  
  <blockquote>
    <p>Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :<br>
    cannot use this function</p>
  </blockquote>
  
  <p>Reproducible sample:</p>
  
  <pre><code>x1 &lt;- 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&lt;-stack(x,y)
  </code></pre>
  
  <p>Can someone please help.</p>