title: How to filter out rows that follow a certain established threshold being reached in R?
body: <p>I have the following data:</p>
  <pre><code>group &lt;- rep(letters[seq(from = 1, to = 3)], each = 4)
  date &lt;- c(&quot;1999-01-01&quot;, &quot;1999-01-02&quot;, &quot;1999-10-01&quot;, &quot;1999-10-05&quot;,
             &quot;1988-02-01&quot;, &quot;1997-12-25&quot;, &quot;1997-12-26&quot;, &quot;1998-01-01&quot;,
             &quot;2000-05-01&quot;, &quot;2000-07-01&quot;, &quot;2000-12-01&quot;, &quot;2000-12-02&quot;)
  day &lt;- c(1,2,274,278,
           1,3616,3617,3623,
           1, 62,215,216)
  diff &lt;- c(0, 1, 272, 4, 
            0, 3615, 1, 6, 
            0, 61, 153, 1)
  matrix &lt;- matrix(c(group, date, day, diff), ncol = 4, byrow = F)
  df &lt;- as.data.frame(matrix)
  colnames(df) &lt;- c(&quot;group&quot;, &quot;date&quot;, &quot;day&quot;, &quot;diff&quot;)
  df
  </code></pre>
  <p>In this case, &quot;diff&quot; is the difference between consecutive dates, in days, by group. I am trying to filter out all rows after an arbitrary threshold of &quot;diff&quot; has been reached. For example, let's say this threshold is a difference of 100 days. I would want to eliminate all rows on and after the first value of &quot;diff&quot; that is greater than 100, by group. In other words, my output would look as follows:</p>
  <pre><code>group2 &lt;- c(&quot;a&quot;, &quot;a&quot;, 
             &quot;b&quot;, 
             &quot;c&quot;, &quot;c&quot;)
  date2 &lt;- c(&quot;1999-01-01&quot;, &quot;1999-01-02&quot;,
            &quot;1988-02-01   &quot;,
            &quot;2000-05-01&quot;, &quot;2000-07-01&quot;)
  day2 &lt;- c(1, 2,
           1,
           1, 62)
  diff2 &lt;- c(0,1,
             0,
             0,61)
  
  matrix2 &lt;- matrix(c(group2, date2, day2, diff2), ncol = 4, byrow = F)
  df2 &lt;- as.data.frame(matrix2)
  colnames(df2) &lt;- c(&quot;group&quot;, &quot;date&quot;, &quot;day&quot;, &quot;diff&quot;)
  df2
  </code></pre>
  <p>Is there some way to get this output? There are similar questions on Stack Overflow, but they do not accommodate groups, or do not work for my data. Filtering any value of &quot;diff&quot; less than 100 is not the solution, as it leaves me with dates that occurred AFTER the 100 day gap, which I do not want.</p>
  <pre><code>df %&gt;%
    filter(diff &lt; 100)
  </code></pre>
  <p>Again, I just want to find the first instance where <code>diff &gt; 100</code> and remove this row and all subsequent rows for that group. Any help here would be appreciated.</p>