[R] how to aggregate T-test result in an elegant way?

arun smartpink111 at yahoo.com
Mon Jan 7 05:21:33 CET 2013


Hi,
You didn't provide any example data.  So, I am not sure whether this helps.

set.seed(15)
dat1<-data.frame(A=sample(10:20,5,replace=TRUE),B=sample(18:28,5,replace=TRUE),C=sample(25:35,5,replace=TRUE),D=sample(20:30,5,replace=TRUE))
 dat2<-dat1[,-1] # I forgot to paste this line
 res<-lapply(lapply(seq_len(ncol(dat2)),function(i)
 t.test(dat2[,i],dat1[,1],paired=TRUE)),function(x) 
data.frame(meanDiff=x$estimate,p.value=x$p.value))# paired
names(res)<-paste("A",LETTERS[2:4],sep="")
res<- do.call(rbind,res)
res
  # meanDiff     p.value
#AB      9.4 0.021389577
#AC     15.0 0.002570261
#AD     10.6 0.003971604


#or
res1<-lapply(lapply(seq_len(ncol(dat2)),function(i)
 t.test(dat2[,i],dat1[,1],paired=FALSE)),function(x) 
data.frame(mean=x$estimate,p.value=x$p.value))
names(res1)<-paste("A",LETTERS[2:4],sep="")
res1<-do.call(rbind,res1)
row.names(res1)[grep("mean
 of 
y",row.names(res1))]<-gsub("(.*\\.).*","\\1A",row.names(res1)[grep("mean
 of y",row.names(res1))])
row.names(res1)[grep("mean of 
x",row.names(res1))]<-gsub("(\\w)(\\w)(\\.).*","\\1\\2\\3\\2",row.names(res1)[grep("mean
 of x",row.names(res1))])
res1
#     mean      p.value
#AB.B 25.2 1.299192e-03
#AB.A 15.8 1.299192e-03
#AC.C 30.8 5.145519e-05
#AC.A 15.8 5.145519e-05
#AD.D 26.4 1.381339e-03
#AD.A 15.8 1.381339e-03


A.K.



----- Original Message -----
From: Yao He <yao.h.1988 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Sunday, January 6, 2013 10:20 PM
Subject: [R] how to aggregate T-test result in an elegant way?

Dear all:

Plan 1:
I want to do serval t-test means for different variables in a loop ,
so I want to add all results to an object then  dump() them to an
text. But I don't know how to append T-test result to the object?

I have already plot the barplot and I want to know an elegant way to
report raw result.
Can anybody give me some pieces of advice?

Yao He
—————————————————————————
Master candidate in 2rd year
Department of Animal genetics & breeding
Room 436,College of Animial Science&Technology,
China Agriculture University,Beijing,100193
E-mail: yao.h.1988 at gmail.com
——————————————————————————

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list