title: Remove rows for each unique identifier based on value of a variable in preceding row (R)
body: <p>I have data in this format</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Name</th>
  <th>SampleDate</th>
  <th>Occasion</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>A</td>
  <td>01/20</td>
  <td>First</td>
  </tr>
  <tr>
  <td>A</td>
  <td>01/21</td>
  <td>Second</td>
  </tr>
  <tr>
  <td>A</td>
  <td>01/22</td>
  <td>Third</td>
  </tr>
  <tr>
  <td>A</td>
  <td>01/25</td>
  <td>Fourth</td>
  </tr>
  <tr>
  <td>B</td>
  <td>01/20</td>
  <td>First</td>
  </tr>
  <tr>
  <td>B</td>
  <td>01/22</td>
  <td>Died</td>
  </tr>
  <tr>
  <td>B</td>
  <td>NA</td>
  <td>Second</td>
  </tr>
  <tr>
  <td>B</td>
  <td>NA</td>
  <td>Third</td>
  </tr>
  <tr>
  <td>B</td>
  <td>NA</td>
  <td>Fourth</td>
  </tr>
  <tr>
  <td>C</td>
  <td>01/22</td>
  <td>First</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>and so on. In many cases, I also have individuals that weren't sampled on intervening occasions (e.g. sampled in First, not in Second and Third, sampled in Fourth), so I'll be inferring values for multiple variables on those occasions where they weren't sampled. I want to be able to filter out the rows that come after an individual (here, B) died, so that I don't have impossible values (clearly I can't have any future values if B has died). The resultant dataset would look like</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Name</th>
  <th>SampleDate</th>
  <th>Occasion</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>A</td>
  <td>01/20</td>
  <td>First</td>
  </tr>
  <tr>
  <td>A</td>
  <td>01/21</td>
  <td>Second</td>
  </tr>
  <tr>
  <td>A</td>
  <td>01/22</td>
  <td>Third</td>
  </tr>
  <tr>
  <td>A</td>
  <td>01/25</td>
  <td>Fourth</td>
  </tr>
  <tr>
  <td>B</td>
  <td>01/20</td>
  <td>First</td>
  </tr>
  <tr>
  <td>B</td>
  <td>01/22</td>
  <td>Died</td>
  </tr>
  <tr>
  <td>C</td>
  <td>01/22</td>
  <td>First</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I'll be using this df to <code>full_join</code> with my main dataset and then infer values.</p>
  <p>I tried creating a new variable <code>mutate(var = ifelse(lag(Occasion,n=1) == &quot;Died&quot;, 0,1))</code> and setting n = 2 or 3 to use for downstream filtering, but this often removes several rows that are needed. I've looked at various similar queries on Stackoverflow, but couldn't find any that seemed to address this specific problem. I'd really appreciate any help in sorting this out. Thanks in advance!</p>