对按因子分割的数据帧中的许多列应用t检验？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (51)

``````Variable    Treated Untreated   p-value    Test-statistic CI of difference****
Conc        0.3450  0.2763          XXX     T           XX - XX
Rate        141.58  110.7272        xxx     T           XX - XX
``````

（嘌呤霉素只包含两个数值变量，但我正在寻找的解决方案适用于具有许多数值变量的数据框）

``````Grouping variable   var1    var2    var3    var4    var5
1           3   5   7   3   7
1           3   7   5   9   6
1           5   2   6   7   6
1           9   5   7   0   8
1           2   4   5   7   8
1           2   3   1   6   4
2           4   2   7   6   5
2           0   8   3   7   5
2           1   2   3   5   9
2           1   5   3   8   0
2           2   6   9   0   7
2           3   6   7   8   8
2           10  6   3   8   0
``````

``````"Mean in group 1"   "Mean in group 2"  "P-value of difference" "N"

var1            ##          ##          ##          ##
var2            ##          ##          ##          ##
var3            ##          ##          ##          ##
var4            ##          ##          ##          ##
var5            ##          ##          ##          ##
``````

2 个回答

``````res <- sapply(split(Puromycin[,-3],  Puromycin\$state), t.test)[c(1:3,5),]
conf.level <- sapply(sapply(split(Puromycin[,-3],  Puromycin\$state), t.test)[4, ], '[', 1:2)
res <- rbind(res, conf.level.lower=conf.level[1,], conf.level.upper=conf.level[2,])
res
treated    untreated
statistic        4.297025   4.206221
parameter        23         21
p.value          0.00026856 0.0003968191
estimate         70.96417   55.50182
conf.level.lower 36.80086   28.06095
conf.level.upper 105.1275   82.94268
``````

``````df <- read.table(text="Group   var1    var2    var3    var4    var5
1           3   5   7   3   7
1           3   7   5   9   6
1           5   2   6   7   6
1           9   5   7   0   8
1           2   4   5   7   8
1           2   3   1   6   4
2           4   2   7   6   5
2           0   8   3   7   5
2           1   2   3   5   9
2           1   5   3   8   0
2           2   6   9   0   7
2           3   6   7   8   8
2           10  6   3   8   0", header = TRUE)

library(matrixTests)

col_t_welch(df[df\$Group==1,-1], df[df\$Group==2,-1])
obs.x obs.y obs.tot   mean.x   mean.y  mean.diff     var.x     var.y   stderr        df  statistic    pvalue  conf.low conf.high alternative mean.null conf.level
var1     6     7      13 4.000000 3.000000  1.0000000  7.200000 11.333333 1.679002 10.963146  0.5955919 0.5635410 -2.696975  4.696975   two.sided         0       0.95
var2     6     7      13 4.333333 5.000000 -0.6666667  3.066667  5.000000 1.106976 10.938135 -0.6022411 0.5592911 -3.104788  1.771454   two.sided         0       0.95
var3     6     7      13 5.166667 5.000000  0.1666667  4.966667  6.666667 1.334226 10.995151  0.1249164 0.9028444 -2.770103  3.103436   two.sided         0       0.95
var4     6     7      13 5.333333 6.000000 -0.6666667 10.666667  8.333333 1.722862 10.146824 -0.3869530 0.7067827 -4.497927  3.164593   two.sided         0       0.95
var5     6     7      13 6.500000 4.857143  1.6428571  2.300000 13.142857 1.503624  8.285649  1.0925986 0.3053172 -1.803808  5.089522   two.sided         0       0.95
``````