Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

R: t-test over all columns

I tried to do t-test to all columns (two at a time) of my data frame, and extract only the p-value. Here is what I have come up with:

for (i in c(5:525) ) {

t_test_p.value =sapply( Data[5:525], function(x) t.test(Data[,i],x, na.rm=TRUE)$p.value)

}

My questions are: 1. is there a way to do it without a loop? 2. how to capture the results of the t-test.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would recommend to convert your data frame to long format and use pairwise.t.test with appropriate p.adjust:

> library(reshape2)
> 
> df <- data.frame(a=runif(100),
+          b=runif(100),
+          c=runif(100)+0.5,
+          d=runif(100)+0.5,
+          e=runif(100)+1,
+          f=runif(100)+1)
> 
> d <- melt(df)
Using  as id variables
> 
> pairwise.t.test(d$value, d$variable, p.adjust = "none")

    Pairwise comparisons using t tests with pooled SD 

data:  d$value and d$variable 

  a      b      c      d      e   
b 0.86   -      -      -      -   
c <2e-16 <2e-16 -      -      -   
d <2e-16 <2e-16 0.73   -      -   
e <2e-16 <2e-16 <2e-16 <2e-16 -   
f <2e-16 <2e-16 <2e-16 <2e-16 0.63

P value adjustment method: none 
> pairwise.t.test(d$value, d$variable, p.adjust = "bon")

    Pairwise comparisons using t tests with pooled SD 

data:  d$value and d$variable 

  a      b      c      d      e
b 1      -      -      -      -
c <2e-16 <2e-16 -      -      -
d <2e-16 <2e-16 1      -      -
e <2e-16 <2e-16 <2e-16 <2e-16 -
f <2e-16 <2e-16 <2e-16 <2e-16 1

P value adjustment method: bonferroni 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...