# [R] T tests on multiple groups

Jim Lemon drjimlemon at gmail.com
Thu Jan 19 22:48:51 CET 2017

```Hi Ed,
It's little hard to work out exactly what you want, but here's a guess:

esdf<-data.frame(GENO=rep(c("control","A","B","AB"),each=20),
age=rep(c(10,20),40),OBS=runif(80,1,21))
for(age in c(10,20)) {
for(geno in c("A","B","AB"))
print(t.test(OBS~GENO,esdf[esdf\$age==age &
esdf\$GENO %in% c("control",geno),]))
}

Note that this is not a good way to use t.test, nor a good way to
analyze data like this. Look at defining sensible contrasts and using
ANOVA or a similar approach.

Jim

On Fri, Jan 20, 2017 at 5:20 AM, Ed Siefker <ebs15242 at gmail.com> wrote:
> I have a data set with observations on groups with multiple variables.
> Let's call them GENO and AGE.  I have control and test genotypes
> and two different ages.  It is only meaningful to compare control and
> test within the same age.
>
> I'd like to get the p value for each group compared back to control
> of the appropriate age.  T-test requires that the grouping factor has
> exactly two levels.   How can I do this efficiently?
>
> I was hoping something like ttest(OBS ~ GENO * AGE, mydata) would work.
> Is there something I can do with tapply() or aggregate() to do this?
> I'd like to end up with a table that looks like this:
>
> GENO    Age    OBS    p.val
> control    10    1.1    1
> control    10    0.9    1
> control    20    2.1    1
> control    20    1.9    1
> A    10    11    0.01224066
> A    10    9    0.01224066
> A    20    21    0.003102783
> A    20    19    0.003102783
> B    10    4    0.057714305
> B    10    6    0.057714305
> B    20    14    0.005923285
> B    20    16    0.005923285
> AB    10    1    0.698488655
> AB    10    1.1    0.698488655
> AB    20    2    0.552786405
> AB    20    2.2    0.552786405
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help