title: Group by and keep rows within dataset by ID where a condition is met and also keep row immediately below if an alternative condition is met
body: <p>Hi I have a dataset such as below:</p>
  <pre><code>df &lt;- 
    read.table(textConnection(&quot;ID   Condition1   Condition2
  A 1 0
  A 1 1 
  A 0 1
  A 1 0
  A 0 0 
  A 0 1
  B 1 1
  B 0 1
  C 1 0
  C 0 0 
  D 1 0
  D 0 1
  D 1 1 
  D 0 0
  &quot;), header=TRUE)
  </code></pre>
  <p>I am looking to group by ID and filter to keep rows based on below conditions:</p>
  <ol>
  <li>If a value of '1' is in column 'Condition1' then the row is retained</li>
  <li>If a value of '1' is in column 'Condition2' then that row is also kept if it is immediately below a row that has a '1' in Condition1 above it.</li>
  </ol>
  <p>Rows may have any number of combinations of 1s and 0s in the two columns. So for the below example, expected output would be:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">ID</th>
  <th style="text-align: center;">Condition1</th>
  <th style="text-align: right;">Condition2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1</td>
  <td style="text-align: right;">0</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">0</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: center;">1</td>
  <td style="text-align: right;">0</td>
  </tr>
  <tr>
  <td style="text-align: left;">B</td>
  <td style="text-align: center;">1</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">B</td>
  <td style="text-align: center;">0</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">C</td>
  <td style="text-align: center;">1</td>
  <td style="text-align: right;">0</td>
  </tr>
  <tr>
  <td style="text-align: left;">D</td>
  <td style="text-align: center;">1</td>
  <td style="text-align: right;">0</td>
  </tr>
  <tr>
  <td style="text-align: left;">D</td>
  <td style="text-align: center;">0</td>
  <td style="text-align: right;">1</td>
  </tr>
  <tr>
  <td style="text-align: left;">D</td>
  <td style="text-align: center;">1</td>
  <td style="text-align: right;">1</td>
  </tr>
  </tbody>
  </table>
  </div>