title: Convert character to variable names used in formula in R
body: <p>I am beginner of R, I met a problem which might be simple for you. Thanks in advance if could give me some help. I am not sure whether the title does reflect the problem I want to ask. To make my problem clear, I will use a simple example.</p>
  
  <p>Let's say we have data frame containing two factors (FE and DI) and three variables (SR1, SR2 and SR3) like:</p>
  
  <pre><code>df&lt;-data.frame(FE=rep(c("FL","FM","FH"),4),DI=rep(c("DL","DH"),each=6),
  SR1=rpois(12,10),SR2=rpois(12,15),SR3=rpois(12,20))
  </code></pre>
  
  <p>I know how to calculate the means of variables according to the factors by using "aggregate", for example:</p>
  
  <pre><code>df.me1&lt;-aggregate(SR1~FE,df,mean)
  df.me2&lt;-aggregate(cbind(SR1,SR2,SR3)~FE+DI,df,mean)
  </code></pre>
  
  <p>Then, I make two characters (vars and facs) consisting of names of the three variables and the two factors: </p>
  
  <pre><code>vars&lt;-c("SR1","SR2","SR3")
  facs&lt;-c("FE","DI")
  </code></pre>
  
  <p>Now, I want to do the calculations in the following formula for some reason</p>
  
  <pre><code>df.me1&lt;-aggregate(vars[1]~facs[1],df,mean)
  df.me2&lt;-aggregate(cbind(vars[1],vars[2],vars[3])~facs[1]+facs[2],df,mean)
  </code></pre>
  
  <p>The codes certainly do not work, so what should I do to make them work in this way?</p>