title: Dynamic filtering in dplyr
body: <p>Let's say I have a data frame <code>df</code> in R, a vector of column names <code>cols</code> of <code>df</code> of unknown length and a list <code>l</code> of length <code>length(cols)</code> whose elements are vectors <code>l[[1]],l[[2]],...,l[[length(cols)]]</code> describing filter conditions for the columns. How can I define a function of <code>cols</code> and <code>l</code> that returns the filtered dataset? In other words I would like to define a function that looks somewhat like this:</p>
  
  <pre><code>library(dplyr)
  
  dynaFilter &lt;- function(cols, l){
     df %&gt;%
        filter(cols[1] %in% l[[1]], cols[2] %in% l[[2]], ...)
  }
  </code></pre>
  
  <p>and does the following:</p>
  
  <pre><code>df = data.frame(a = c("Hello", "Hi", "Bye"), 
                  b = c("John", "Jack", "John"), 
                  c = c("Mum", "Dad", "Dad"))
  
  dynaFilter(c("a", "c"), list(c("Hi", "Bye"), c("Dad")))
  # returns the same as
  # df %&gt;%
  #    filter(a %in% c("Hi", "Bye"), c %in% c("Dad"))
  
  dynaFilter(c("a", "b", "c"), list(c("Hi", "Bye"), c("Jack") ,c("Dad")))
  # returns the same as
  # df %&gt;%
  #    filter(a %in% c("Hi", "Bye"), b %in% c("Jack"), c %in% c("Dad"))
  </code></pre>