title: filter observations up to less or equal to specific value
body: <p>I have the following data, <code>df</code>. The <code>status</code> will be filled with a <code>stop</code> at the last value of days &lt;=6 and I only keep observations up to <code>days &lt;=6</code> or up to <code>status=Stop</code> per id. For example, if days are equal to 2,5,7 etc, the cut point will be at 5 as the last logical operator will TRUE at days&lt;=6 is 5.</p>
  <pre><code>df &lt;- data.frame(id=c(1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4),
  days=c(1,3,3,4,6,10,11,0,2,3,17,19,20,2,2,4,5,7,9,2,13,15),
  status=c('live','live','live','live','live','live','live','live','live','live','live','To','die','live','To','die',
  'live','live','live','live','live','To'))
  </code></pre>
  <p>i.e.,</p>
  <pre><code>   id days status
  1   1    1   live
  2   1    3   live
  3   1    3   live
  4   1    4   live
  5   1    6   live # 6&lt;=6 True, 
  6   1   10   live # But 10&lt;=6 False, So status should stop at 5th obs.
  7   1   11   live
  8   2    0   live
  9   2    2   live
  10  2    3   live # status should stop at day=3 as 6&lt;=17 is False in the next follow-up.  
  11  2   17   live
  12  2   19     To
  13  2   20    die
  14  3    2   live
  15  3    2   live
  16  3    4   live
  17  3    5   live # 5&lt;=6 True 
  18  3    7   live # 7&lt;=6 false, so stop should at maximum of True (at day=5)
  19  3    9   live
  20  4    2   live  # True
  21  4   13   live  # false
  22  4   15     To
  
  </code></pre>
  <p>Expected output</p>
  <pre><code>   id days status
  1   1    1   live
  2   1    3   live
  3   1    3   live
  4   1    4   live
  5   1    6   Stop
  6   2    0   live
  7   2    2   live
  8   2    3   Stop
  9   3    2   live
  10  3    2     To
  11  3    4    die
  12  3    5   Stop
  13  4    2   Stop
  </code></pre>
  <p>I am trying using</p>
  <pre><code>df %&gt;%  group_by(id) %&gt;%  
    mutate(status = case_when(days &gt;=6  ~ &quot;Stop&quot;,  TRUE ~ status)) %&gt;% 
    slice(seq_len(min(which(status==&quot;Stop&quot;), n())))
  </code></pre>