title: Why the t.test does not work inside this function in R?
body: <p>I am creating a function for conducting t.test on various columns of a data frame. Following is an example with the Orange data set in R:</p>
  
  <pre><code>&gt; t.test(Orange[Orange$Tree=="1",]$age, 
  +        Orange[Orange$Tree=="2",]$age)
  
      Welch Two Sample t-test
  
  data:  Orange[Orange$Tree == "1", ]$age and Orange[Orange$Tree == "2", ]$age
  t = 0, df = 12, p-value = 1
  alternative hypothesis: true difference in means is not equal to 0
  95 percent confidence interval:
   -609.832  609.832
  sample estimates:
  mean of x mean of y 
   922.1429  922.1429 
  </code></pre>
  
  <p>But when this is done inside the following function it doesn't work:</p>
  
  <pre><code>my.t.test &lt;- function(var, df){
    df &lt;- na.omit(df)
    tt1 &lt;- t.test(df[df$Tree=="1",]$var, 
                  df[df$Tree=="2",]$var)
    tt1
  }
  
  &gt; my.t.test(age, Orange)
  Error in var(x) : 'x' is NULL
  In addition: Warning messages:
  1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
  2: In mean.default(x) : argument is not numeric or logical: returning NA
  Called from: var(x)
  </code></pre>
  
  <p>What am I doing wrong?</p>