[R] t test on data frame

Dieter Menne dieter.menne at menne-biomed.de
Fri Nov 4 11:57:00 CET 2005


Bart Joosen <bartjoosen <at> hotmail.com> writes:
> I have constructed a dataframe as follows:
> 
> Oil <- rep(c("Oil1","Oil2","Oil3"),8)
> Comp <- rep(rep(c("C1","C2"),c(4,4)),3)
> Mth <- rep(c("M1","M1","M2","M2"),6)
> Meas <- rep(c(1,2),12)
> Result <- rnorm(24,mean=100, sd=5)
> df <- data.frame(Oil, Comp, Mth, Meas, Result)
> 
> The same compound (Comp) is found in different oils.
> Now I want to see if there are differences in method per compound.
> I tried this one:
> df.t <- t.test(Result~Mth | Comp, df)
> 
> But I get:
> Error in t.test.formula(Result ~ Mth | Comp, df) :
>          grouping factor must have exactly 2 levels
> In addition: Warning message:
> | not meaningful for factors in: Ops.factor(Mth, Comp)
> 

The Docs say:

>The formula interface is only applicable for the 2-sample tests. 

So the grouping cannot by used like one could expect to say "do it by groups". 
You must do it manually, for example:

by(df, df$Comp, function(x) t.test(Result~Mth,x))

See docs of "by" how to extract parameters from the results instead of 
printing. And don't forget multtest, if you superiors ask for all the 300 
compounds your company produces against each other.

Dieter




More information about the R-help mailing list