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

Yao He yao.h.1988 at gmail.com
Mon Jan 7 05:21:57 CET 2013

```Thank you，it is really helpful everytime.

I didn't provide any example data because I thought it is just a
question of how to report t.test() result in R.
However,as you say,it is better to show more details for finding an elegant way

In fact  I generate a 3-dimension array like that:
str(a)
num [1:2, 1:245, 1:3] 47.5 NA 48.9 NA 47.5 ...
- attr(*, "dimnames")=List of 3
..\$ : chr [1:2] "13%" "21%"
..\$ : chr [1:245] "TWF2H101" "TWF2H105" "TWF2H106" "TWF2H110" ...
..\$ : chr [1:3] "EW.INCU" "EW.17.5" "EMW"

I want to do two sample mean t-test between 13% and 21% for each
variable "EW.INCU" "EW.17.5" "EMW".

So I try these codes:
variable<-dimnames(a)[[3]]
O2<-dimnames(a)[[1]]
for (i in variable) {
print(i)
print(O2[1])
print(O2[2])
print(t.test(a[O2[1],,i],a[O2[2],,i],na.rm=T))
}

I don't think it is an elegant way and I am inexperience to report raw result.
Could you give me more help?

Yao He

2013/1/7 arun <smartpink111 at yahoo.com>:
> 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))
>  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.
>

--
―――――――――――――――――――――――――
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
――――――――――――――――――――――――――

```