Compute one sample t-test for each column of a data frame and summarize results in a table

只谈情不闲聊 提交于 2019-12-06 05:53:26

Try something like this and then extract the results you want from the resulting table:

results <- lapply(mydf, t.test)
resultsmatrix <- do.call(cbind, results)
resultsmatrix[c("statistic","estimate","p.value"),]

Gives you:

          A         B          C            D           E           
statistic 1.401338  2.762266   5.406704     3.409422    5.024222    
estimate  1.677863  2.936304   5.418812     4.231458    5.577681    
p.value   0.1772363 0.01240057 3.231568e-05 0.002941106 7.531614e-05

a data.table solution :

library(data.table)
DT <- as.data.table(mydf)
DT[,lapply(.SD,function(x){
         y <- t.test(x)
         list(p = round(y$p.value,2),
              h = round(y$conf.int,2),
              mm = round(y$estimate,2))})]

           A          B         C         D         E
1:        0.2       0.42      0.01         0         0
2: -0.91,3.98 -1.15,2.62 1.19,6.15 2.82,6.33 2.68,6.46
3:       1.54       0.74      3.67      4.57      4.57
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!