title: Finding Row number of Consecutive decreasing values in data
body: <p>I need to detect from data the first element of the first sequence of length 5 of consecutively decreasing numbers. There is a similar <a href="https://stackoverflow.com/questions/19337105/get-first-element-of-the-first-sequence-of-length-n-of-consecutively-increasing?rq=1">post</a> here but when I applied to my data it failed.</p>
  
  <pre><code>set.seed(201)  
  az &lt;- c(sort(runif(10,0,0.9),decreasing = T),sort(runif(3,-0.3,0),decreasing = T),sort(runif(3,-0.3,0),decreasing = F),sort(runif(4,-0.3,0),decreasing = T),sort(runif(4,-0.3,0),decreasing = F),sort(runif(6,-0.3,0),decreasing = T))   
  tz &lt;- seq(1,length(az))
  df &lt;- data.frame(tz,az=round(az,2))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/PALsa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PALsa.png" alt="enter image description here"></a></p>
  
  <p>In the figure above it would be somewhere around tz = 25. </p>
  
  <p>The post says that this function need to improve and so far I cannot get my desired result!</p>
  
  <pre><code>  getFirstBefore&lt;-function(x,len){
      r&lt;-rle(sign(diff(x)))
      n&lt;-which(r$lengths&gt;=len &amp; r$values&lt;0)
      if(length(n)==0)
        return(-1)
      1+sum(r$lengths[seq_len(n[1]-1)])
    }
  
  df1 &lt;- df%&gt;%
  mutate(cns_tz=getFirstBefore(az,5))
  
   tz    az cns_tz
  #1   1  0.56      4
  #2   2  0.55      4
  #3   3  0.33      4
  #4   4  0.33      4
  #5   5  0.26      4
  #6   6  0.15      4
  #7   7  0.12      4
  #8   8  0.09      4
  #9   9  0.04      4
  #10 10  0.04      4
  #11 11 -0.10      4
  #12 12 -0.12      4
  #13 13 -0.16      4
  #14 14 -0.16      4
  #15 15 -0.14      4
  #16 16 -0.14      4
  #17 17 -0.13      4
  #18 18 -0.15      4
  #19 19 -0.22      4
  #20 20 -0.30      4
  #21 21 -0.12      4
  #22 22 -0.12      4
  #23 23 -0.11      4
  #24 24 -0.07      4
  #25 25 -0.05      4
  #26 26 -0.09      4
  #27 27 -0.10      4
  #28 28 -0.15      4
  #29 29 -0.17      4
  #30 30 -0.22      4
  </code></pre>