title: Using paste() within t.test() in user defined R function
body: <p>I am trying to write a function which accepts <code>Var1</code> and <code>Var2</code> from user and run the <code>t.test</code> and return the mean for female classification. But I am getting an error for the calc line. If I run the program without the paste and <code>as.formula</code> functions and run with <code>t.test(dat[[Var2]]~dat[[Var1]]</code> I will get the correct answer.</p>
  
  <p>But in my original code I am required to use the paste function. Could anyone let me know what is the mistake in the code below using paste and as.formula functions? I am using the quine dataframe from MASS library.</p>
  
  <pre><code>func = function(dat=quine,Var1,Var2){
    # calc = t.test(dat[[Var2]]~dat[[Var1]] #gives the answer
    calc = t.test(as.formula(paste(dat[[Var2]], dat[[Var1]], sep="~"))) #gives an error
    return(F.mean = calc$estimate[1])
  }
  
  func(Var1= "Sex", Var2= "Days")
  </code></pre>
  
  <p>Here is the head(quine)</p>
  
  <pre><code>Eth Sex Age Lrn Days
  </code></pre>
  
  <p><code>1   A   M  F0  SL    2</code></p>
  
  <p><code>2   A   M  F0  SL   11</code></p>
  
  <p><code>3   A   M  F0  SL   14</code></p>
  
  <p><code>4   A   M  F0  AL    5</code></p>
  
  <p><code>5   A   M  F0  AL    5</code></p>
  
  <p><code>6   A   M  F0  AL   13</code></p>