【发布时间】:2018-08-01 03:42:58
【问题描述】:
我尝试对数据框的许多列执行独立 t 检验。例如,我创建了一个数据框
set seed(333)
a <- rnorm(20, 10, 1)
b <- rnorm(20, 15, 2)
c <- rnorm(20, 20, 3)
grp <- rep(c('m', 'y'),10)
test_data <- data.frame(a, b, c, grp)
为了运行测试,我使用了with(df, t.test(y ~ group))
with(test_data, t.test(a ~ grp))
with(test_data, t.test(b ~ grp))
with(test_data, t.test(c ~ grp))
我想要这样的输出
mean in group m mean in group y p-value
9.747412 9.878820 0.6944
15.12936 16.49533 0.07798
20.39531 20.20168 0.9027
我想知道如何使用
1.for loop
2.apply()
3. 或许dplyr
此链接R: t-test over all columns 是相关的,但它已有 6 年历史。也许有更好的方法来做同样的事情。
【问题讨论】: