title: Getting strings recognized as variable names in R
body: <p>Related: <a href="https://stackoverflow.com/questions/3770589/strings-as-variable-references-in-r">Strings as variable references in R</a><br>
  Possibly related: <a href="https://stackoverflow.com/questions/5531238/concatenate-expressions-to-subset-a-dataframe">Concatenate expressions to subset a dataframe</a></p>
  
  <hr>
  
  <p>I've simplified the question per the comment request. Here goes with some example data.</p>
  
  <pre><code>dat &lt;- data.frame(num=1:10,sq=(1:10)^2,cu=(1:10)^3)
  set1 &lt;- subset(dat,num&gt;5)
  set2 &lt;- subset(dat,num&lt;=5)
  </code></pre>
  
  <p>Now, I'd like to make a bubble plot from these. I have a more complicated data set with 3+ colors and complicated subsets, but I do something like this:</p>
  
  <pre><code>symbols(set1$sq,set1$cu,circles=set1$num,bg="red")
  symbols(set2$sq,set2$cu,circles=set2$num,bg="blue",add=T)
  </code></pre>
  
  <p>I'd like to do a for loop like this:</p>
  
  <pre><code>colors &lt;- c("red","blue")
  sets &lt;- c("set1","set2")
  vars &lt;- c("sq","cu","num")
  
  for (i in 1:length(sets)) {
     symbols(sets[[i]][,sq],sets[[i]][,cu],circles=sets[[i]][,num],
     bg=colors[[i]],add=T)
  }    
  </code></pre>
  
  <p>I know you can have a variable evaluated to specify the column (like <code>var="cu"; set1[,var]</code>; I want to know how to get a variable to specify the data.frame itself (and another to evaluate the column).</p>
  
  <hr>
  
  <p><strong>Update:</strong> Ran across <a href="http://www.r-bloggers.com/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/" rel="noreferrer">this post</a> on r-bloggers which has this example:</p>
  
  <pre><code>x &lt;- 42
  eval(parse(text = "x"))
  [1] 42
  </code></pre>
  
  <p>I'm able to do something like this now:</p>
  
  <pre><code>eval(parse(text=paste(set[[1]],"$",var1,sep="")))
  </code></pre>
  
  <p>In fiddling with this, I'm finding it interesting that the following are not equivalent:</p>
  
  <pre><code>vars &lt;- data.frame("var1","var2")
  eval(parse(text=paste(set[[1]],"$",var1,sep="")))
  eval(parse(text=paste(set[[1]],"[,vars[[1]]]",sep="")))
  </code></pre>
  
  <p>I actually have to do this:</p>
  
  <pre><code>eval(parse(text=paste(set[[1]],"[,as.character(vars[[1]])]",sep="")))
  </code></pre>
  
  <p><strong>Update2:</strong> The above works to output values... but not in trying to plot. I can't do:</p>
  
  <pre><code>for (i in 1:length(set)) {
  symbols(eval(parse(text=paste(set[[i]],"$",var1,sep=""))),
         eval(parse(text=paste(set[[i]],"$",var2,sep=""))),
         circles=paste(set[[i]],".","circles",sep=""),
         fg="white",bg=colors[[i]],add=T)
  }
  </code></pre>
  
  <p>I get <code>invalid symbol coordinates</code>. I checked the class of set[[1]] and it's a factor. If I do <code>is.numeric(as.numeric(set[[1]]))</code> I get <code>TRUE</code>. Even if I add that above prior to the <code>eval</code> statement, I still get the error. Oddly, though, I can do this:</p>
  
  <pre><code>set.xvars &lt;- as.numeric(eval(parse(text=paste(set[[i]],"$",var1,sep=""))))
  set.yvars &lt;- as.numeric(eval(parse(text=paste(set[[i]],"$",var2,sep=""))))
  symbols(xvars,yvars,circles=data$var3)
  </code></pre>
  
  <p>Why different behavior when stored as a variable vs. executed within the symbol function?</p>