[R] t.test data in one column

Erik Iverson eriki at ccbr.umn.edu
Thu Apr 1 21:02:30 CEST 2010


Hello,

Marlin Keith Cox wrote:
> I need a two sample t.test between M and F.  The data are arranged in one
> column, x.  Cant seem to figure how to run a two sample t.test.  Not really
> sure what this output is giving me, but there should be no difference
> between M and F in the example, but summary p-value indicates this.
> 
> How can I run a two sample t.test with data in one column.
> 
> x=rep(c(1,2,3,4),2)
> y=rep(c("M","M","M","M","F","F","F","F"))
> data<-cbind(x,y)
> t.test(x,by=list(y))

Several issues:

First, your usage of cbind makes 'data' a matrix of type character, R no 
longer sees your numeric x.  You most likely want a data.frame (Which 
can contain multiple types) instead of a matrix (which has one type of 
data), so replace line 3 (and "data" is a function and argument name, so 
let's call it something else) with

df <- data.frame(x, y)

I don't see the "by" argument documented anywhere in ?t.test.

I do see the "formula" argument, documented as:

  formula: a formula of the form ‘lhs ~ rhs’ where ‘lhs’ is a numeric
           variable giving the data values and ‘rhs’ a factor with two
           levels giving the corresponding groups.


So try,

t.test(x ~ y, data = df)



More information about the R-help mailing list