我想对数据表中除分组变量和 Obs.Number 之外的所有变量的两个子组应用均值 t 检验和中位数 Wilcoxon 检验。我有两个组:组 0 和组 1。我的数据如下所示:
Obs.Number Var1 Var2 Var3 Var4 Var5 Var6 GroupingVar
1 32153.00 48516.00 0.3687 0.0160 12.4288 28646.0 0
2 22465.00 34494.00 0.1804 0.0213 10.5003 20988.0 1
3 1393.10 4257.40 10.1413 0.0359 53.2323 127.3 1
4 0.99 164.07 0.4906 0.0817 -989.5800 0.0 1
5 4745.60 5712.80 0.4914 0.1766 62.5905 2488.7 1
6 10222.80 9898.20 0.8993 0.0115 15.6440 7665.1 0
...
现在我想对所有变量应用这两个测试以获得如下所示的输出:
Test-Statistic Test-Statistic p-value p-value
for Mean T-test for Wilcoxon Median-test of Mean-test of Median-test
Variable 1 ? ? ? ?
Variable 2 ? ? ? ?
...
我将 t.test() 和 wilcox.test() 与 sapply() 一起使用,但没有成功。任何想法如何得到结果?
最佳答案
这样就完成了工作(尽管非常不优雅):
## using am as the grouping variable
data.frame(stats_t = apply(subset(mtcars, select=-am), 2, function(y) t.test(y ~ mtcars$am)$statistic),
p_t = apply(subset(mtcars, select=-am), 2, function(y) t.test(y ~ mtcars$am)$p.value),
stats_w = apply(subset(mtcars, select=-am), 2, function(y) wilcox.test(y ~ mtcars$am, exact = FALSE)$statistic),
p_w = apply(subset(mtcars, select=-am), 2, function(y) wilcox.test(y ~ mtcars$am, exact = FALSE)$p.value)
)
stats_t p_t stats_w p_w
mpg -3.7671231 1.373638e-03 42.0 1.871391e-03
cyl 3.3541138 2.464713e-03 194.0 3.899814e-03
disp 4.1977266 2.300413e-04 214.0 5.493451e-04
hp 1.2661888 2.209796e-01 176.0 4.570132e-02
drat -5.6460883 5.266742e-06 24.0 1.426919e-04
...
关于r - 对表中的多个变量应用 t 检验和 Wilcoxon 检验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25383570/