title: using case_when() and filter() to subset a dataframe based on values in one column and levels in another column in R
body: <p>I want to filter (extract rows from) a dataframe based on values from one column while making sure all rows with the same level as the one I extracted are also extracted. Example:</p>
  <pre><code>condition&lt;- rep(c(&quot;c1&quot;, &quot;c2&quot;, &quot;c3&quot;, &quot;c4&quot;), times = 4)
  levelled &lt;- c(rep(&quot;a&quot;,times = 4), rep(&quot;b&quot;, times = 4), rep(&quot;c&quot;, times = 4), rep(&quot;d&quot;, times = 4))
  direction &lt;- c(rep(&quot;up&quot;, times=10), rep(&quot;down&quot;, times = 1), rep(&quot;up&quot;, times = 5))
  
  df &lt;- data.frame(condition, levelled, direction)
  </code></pre>
  <p>This results in this dataframe:</p>
  <pre><code>   condition levelled direction
  1         c1        a        up
  2         c2        a        up
  3         c3        a        up
  4         c4        a        up
  5         c1        b        up
  6         c2        b        up
  7         c3        b        up
  8         c4        b        up
  9         c1        c        up
  10        c2        c        up
  11        c3        c      down
  12        c4        c        up
  13        c1        d        up
  14        c2        d        up
  15        c3        d        up
  16        c4        d        up
  </code></pre>
  <p>I am only interested in <code>direction == &quot;down&quot; </code>, but I want to extract all rows that have the same level in the <code>levelled</code> column. So my desired output df is this:</p>
  <pre><code>desired_output 
     condition levelled direction
  9         c1        c        up
  10        c2        c        up
  11        c3        c      down
  12        c4        c        up
  </code></pre>
  <p>In my <code>desired_output</code> dataframe, I extracted the row with <code>direction == down</code> but also the other 3 rows that have the same level in the <code>levelled</code> column.
  I think I should try something like this, but I don't know what to write on the right hand side of the tilde sign:</p>
  <pre><code>desired_output &lt;- df %&gt;% fiter(
    case_when(
      direction == &quot;up&quot; ~ #??
    )
  )
  </code></pre>