title: Return rows where consecutive values meet criterion - include values in previous rows
body: <p>I would like to follow up on my recent <a href="https://stackoverflow.com/questions/54132300/return-rows-where-consecutive-values-meet-criterion">question</a></p>
  
  <p>I have the following database and I am looking for rows which have a certain number consecutive values exceeding certain threshold, example:</p>
  
  <p>In the following dataframe <code>df</code>. I would like to return a vector <code>result</code> that indicates which rows meet the following criterion: at least 5 consecutive values <code>consecutiveentries</code> in that row are lower than <code>threshold</code> -1.2.</p>
  
  <pre><code>set.seed(125)
  
  df &lt;- data.frame(V1=rnorm(10,-1.5,.5),
                   V2=rnorm(10,-1.5,.5),
                   V3=rnorm(10,-1.5,.5),
                   V4=rnorm(10,-1.5,.5),
                   V5=rnorm(10,-1.5,.5),
                   V6=rnorm(10,-1.5,.5),
                   V7=rnorm(10,-1.5,.5),
                   V8=rnorm(10,-1.5,.5),
                   V9=rnorm(10,-1.5,.5),
                   V10=rnorm(10,-1.5,.5))
  rownames(df) &lt;- c(seq(1976,1985,1))
  
  threshold = -1.2
  consecutiveentries = 5
  number &lt;- consecutiveentries-1
  
  result &lt;- as.numeric(names(which(apply(df, 1, function(x) with(rle(x &lt; threshold), any(lengths[values] &gt; number))))))
  
  result
  [1] 1985
  </code></pre>
  
  <p>Now I would like to take it step further. I would like to check for 5 consecutive numbers <code>consecutiveentries</code> with no row end limit. In other words, if in preceding row there are values below the <code>threshold</code> - for example 5 values are lower than <code>threshold</code> -1.2 at the end of row 1976 and 2 first values are below the <code>threshold</code> at the beginning of row 1977, then I would like to have a solution for three options. Either return the name of the second row where 5 consecutive values across two rows meet the criteria as <code>result2</code>, or the name of the first row as <code>result1</code>. Finally, return both values as <code>result3</code>.</p>
  
  <p>Results would be following:</p>
  
  <pre><code>result1
      [1] 1976 1980 1983 1985
  result2
      [1] 1977 1981 1984 1985
  result3
      [1] 1976 1977 1980 1981 1983 1984 1985
  </code></pre>