title: R how to automate a filter for unknown variables
body: <p>I have a dataframe and a list. The list contains the data I need to filter in the dataframe. How can I automate the filtering process when I don't know the variables in the list?</p>
  <p>some sample data:</p>
  <pre><code>df &lt;- data.frame(V1 = c(sample(1:2,10,replace=T)),
                   V2 = c(sample(c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;),10, replace=T)),
                   V3 = c(sample(100:104,10,replace=T)))
  </code></pre>
  <p>The list, <code>f_list</code>, is created in another part of the application and eventually passed on to the function that needs to do the filtering. For example, some times the list contains V1 and V3</p>
  <pre><code>f_list &lt;- list()
  f_list$V1 &lt;- c(&quot;2&quot;)
  f_list$V3 &lt;- c(&quot;101&quot;,&quot;103&quot;,&quot;104&quot;)
  </code></pre>
  <p>Other times it contains V1 and V2</p>
  <pre><code>f_list &lt;- list()
  f_list$V1 &lt;- c(&quot;1&quot;)
  f_list$V2 &lt;- c(&quot;A&quot;,&quot;B&quot;)
  </code></pre>
  <p>and so on... the real data has hundreds of variables. How can I automate the filtering process that would look something like this when the variables are known?</p>
  <pre><code>df %&gt;% 
    filter(V1 %in% f_list$V1,
           V3 %in% f_list$V3)
  </code></pre>
  <p>How do I construct the loop?</p>
  <p><strong>EDITED</strong>
  I edited the name of the object, from <code>ls</code> to <code>f_list</code> per @I_0's reminder that objects should not have names of existing functions.</p>