title: Convert character argument with multiple conditional statements into logical to subset in a function independent of dataframe name
body: <p><strong><em>Statement of problem:</em></strong> I am creating a function that needs to subset the data based on sub-population (subpop) arguments provided by the user; one or more conditions may be supplied as a character, e.g., <code>subpop = "sex ==1 &amp; race4 == 2"</code>.</p>
  
  <p>The relevant function (excerpts) looks something like: </p>
  
  <pre><code>collapse = function(data, subpop,...) {
  data2 = subset(data, subpop[1])
  ...
  }
  </code></pre>
  
  <p><strong><em>Sample dataset created with the</em></strong> <code>dput</code> <strong><em>function</em></strong>: </p>
  
  <pre><code>    structure(list(sex = structure(c(0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 
  1, 0, 1, 0, 1, 0, 0, 1, 0, 0), labels = c(male = 0, female = 1
  ), class = "labelled"), race4 = structure(c(2, 3, 3, 1, 2, 2, 
  2, 2, 1, 3, 3, 1, 2, 1, 3, 2, 2, 1, 2, 2), labels = c(MexAm = 1, 
  white = 2, black = 3, other = 4), class = "labelled")), row.names = c(NA, 
  -20L), class = c("tbl_df", "tbl", "data.frame"))
  </code></pre>
  
  <p><strong><em>My attempts so far:</em></strong> I can easily subset the data if I supplied the arguments as a logical statement directly rather than passing it through an object. </p>
  
  <pre><code>&gt; data2 = subset(data, sex ==1 &amp; race4 == 2) #Attempt 1: Works
  &gt; dim(data2)
  [1] 3 2
  
  &gt; subpop &lt;- "sex ==1 &amp; race4 == 2" #Attempt 2: Does not work
  &gt; y = with(data, subpop[1])
  &gt; data2 = subset(data, y)
  Error in subset.data.frame(data, y) : 'subset' must be logical
  
  &gt; y = with(data, subpop[1]) #Attempt 3: Does not work; empty rows 
  &gt; data2 = subset(data, as.logical(y))
  &gt; dim(data2)
  [1] 0 2
  
  &gt; data2=data[which(is.logical(subpop[1])),] #Attempt 4: Does not work; empty rows
  &gt; dim(data2)
  [1] 0 2
  
  &gt; data2 = subset(data, pander::evals(subpop)) #Attempt 5: Does not work
  Error in subset.data.frame(n, evals(subpop)) : 'subset' must be logical
  
  &gt; eval(parse(text == subpop)) #Attempt 6: Does not work
  Error in parse(text == subpop) : unused argument (text == subpop)
  </code></pre>
  
  <p>These are just a few of dozens of code I have tried. Over the past few days, I have combed through countless forums, discussions, and posts, but can't find something. Maybe I am missing something somewhere. This is my first post on this forum and I really hope someone will help me out here.</p>
  
  <p>The fundamental issue here is that I can't seem to be able to get R to recognize the contents of the subgroup object as a logical statement, no matter what I do.</p>
  
  <p><strong><em>Summary</em></strong>: How do I pass an object containing a set of conditions so that R can use the contents of that object to subset. The number of conditions may be very variable in their number, format, or types of operators used (e.g., "==", "%in%", ">=", etc). There is no way of knowing this a priori, as such, I need to pass those conditions via an object rather than directly. Many thanks in advance.</p>