title: Filtering multiple columns of data frame inside a loop in R
body: <p>I want to use a loop to filter multiple columns of a data frame, removing rows where any of the given column values are in a particular list.</p>
  <p>For instance:</p>
  <pre><code>&gt; my_df &lt;- data.frame(word1 = c(&quot;one&quot;, &quot;two&quot;, &quot;red&quot;, &quot;blue&quot;), word2 = c(&quot;apple&quot;,&quot;orange&quot;,&quot;banana&quot;,&quot;pear&quot;), word3 = c(&quot;red&quot;, &quot;orange&quot;, &quot;yellow&quot;, &quot;green&quot;))
  &gt; color_words = c(&quot;red&quot;, &quot;orange&quot;, &quot;yellow&quot;, &quot;green&quot;, &quot;blue&quot;)
  &gt; my_df
    word1  word2  word3
  1   one  apple    red
  2   two orange orange
  3   red banana yellow
  4  blue   pear  green
  </code></pre>
  <p>Using the dplyr <code>filter()</code> function:</p>
  <pre><code>&gt; my_df %&gt;% filter(!word1 %in% color_words) %&gt;% filter(!word2 %in% color_words)
    word1 word2 word3
  1   one apple   red
  </code></pre>
  <p>My first attempt to perform this filtering in a loop was:</p>
  <pre><code>col_names &lt;- c(&quot;word1&quot;,&quot;word2&quot;)
  for(col in col_names){
      my_df &lt;- my_df %&gt;% filter(!col %in% color_words)
  }
  &gt; my_df
    word1  word2  word3
  1   one  apple    red
  2   two orange orange
  3   red banana yellow
  4  blue   pear  green
  </code></pre>
  <p>I read about <a href="https://tidyeval.tidyverse.org/dplyr.html#enquo-and---quote-and-unquote-arguments" rel="nofollow noreferrer">quoting and unquoting</a> when using <code>filter()</code>, so I also tried:</p>
  <pre><code>for(col in col_names){
      col &lt;- enquo(col)
      my_df &lt;- my_df %&gt;% filter(!UQ(col) %in% color_words)
  }
  &gt; my_df
    word1  word2  word3
  1   one  apple    red
  2   two orange orange
  3   red banana yellow
  4  blue   pear  green
  </code></pre>
  <p>and</p>
  <pre><code>for(col in col_names){
      my_df &lt;- my_df %&gt;% filter(!UQ(col) %in% color_words)
  }
  &gt; my_df
    word1  word2  word3
  1   one  apple    red
  2   two orange orange
  3   red banana yellow
  4  blue   pear  green
  </code></pre>
  <p>What is the correct way to go about doing this filtering via a loop?</p>