Skip to content

t test on data frame

2 messages · Bart Joosen, Dieter Menne

#
Hi,

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)

How can I get my results like:
Comp1: Mean Mth 1: ....
           Mean Mth 2: ...
           t = ....
Comp 2: ....
....

Is this possible?
And please: How can I do it?

Kind regards

Bart
#
Bart Joosen <bartjoosen <at> hotmail.com> writes:
The Docs say:
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