R: t-test over all columns

后端 未结 5 766
醉话见心
醉话见心 2020-11-28 14:22

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) ) {
         


        
5条回答
  •  Happy的楠姐
    2020-11-28 14:48

    I run this:

    tres<-apply(x,1,t.test)
    pval<-vapply(tres, "[[", 0, i = "p.value")
    

    It took me a while to divine the "vapply" trick to pull the pvals out of the t.test result object list. (I edited this from 'sapply' because of Henrik's comment below)

    If it's a paired t-test, you can just subtract and test for means=0, which gives exactly the same result (that's all a paired t.test is):

    tres<-apply(y-x,1,t.test)
    pval<-vapply(tres, "[[", 0, i = "p.value")
    

    Again this is a per-row t-test over all columns.

提交回复
热议问题