[英]How to loop through columns in R to do a T test?
我有一列帶有響應變量,許多列帶有自變量。 每個自變量只是二進制 0 或 1,我想遍歷每一列以計算 1 的響應變量平均值和 0 的響應變量平均值,以便我可以運行 T 檢驗。 我是 R 的新手,不知道如何將響應變量列放在一邊或如何將所有其他列分配給變量。
您可以使用lapply
來解決您的問題:
preds <- a vector of all predictors
response <- "the_response"
lapply(preds, function(x) t.test(reformulate(x, response), data = your_data))
例子:
dat
y x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
1 5.1 1 1 1 1 1 1 1 1 1 1
2 4.9 1 1 1 1 0 1 1 1 1 1
3 4.7 0 0 0 0 1 0 0 0 0 0
4 4.6 0 1 1 0 0 0 1 1 0 0
5 5.0 0 1 1 1 1 1 0 0 1 1
6 5.4 1 1 1 1 1 1 1 1 1 1
7 4.6 1 0 1 1 0 0 1 0 1 0
8 5.0 0 0 0 1 0 0 0 0 0 0
9 4.4 0 0 0 0 0 0 0 0 1 0
10 4.9 0 1 0 0 0 0 1 1 0 0
11 5.4 1 1 1 1 1 1 1 1 0 1
12 4.8 1 1 1 1 1 1 1 1 1 1
13 4.8 0 1 0 0 0 0 0 0 1 0
14 4.3 1 1 1 1 1 1 1 1 1 1
15 5.8 0 0 1 1 1 0 1 1 1 0
preds <- names(dat)[-1]
response <- "y"
lapply(preds, function(x) t.test(reformulate(x, response), data = dat, var.equal = TRUE))
[[1]]
Two Sample t-test
data: y by x1
t = -0.13376, df = 13, p-value = 0.8956
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.4900214 0.4328786
sample estimates:
mean in group 0 mean in group 1
4.900000 4.928571
[[2]]
Two Sample t-test
data: y by x2
t = -0.088442, df = 13, p-value = 0.9309
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.5085418 0.4685418
sample estimates:
mean in group 0 mean in group 1
4.90 4.92
:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.