【发布时间】:2016-10-21 20:37:54
【问题描述】:
我无法在循环内创建prop.test 结果的数据框。我正在循环两个向量,它们指定运行多个prop.tests 所需的变量。我可以打印结果,但我想将结果放入数据框中。
样本数据:
set.seed(1234)
tc <- sample(c("test", "control"), 1000, replace = TRUE, prob = c(.8, .2))
target <- sample(LETTERS[1:2], 1000, replace = TRUE, prob=c(1/3, 1/3, 1/3))
pa <- sample(c(0, 1), 1000, replace = TRUE)
sc <- sample(c(0, 1), 1000, replace = TRUE)
ig <- sample(c(0, 1), 1000, replace = TRUE)
test <- data.frame(tc, target, pa, sc, ig)
使用变量运行prop.test 循环:
#define loop variables
target_var <- c("A", "B") #targets
metric <- c("pa", "sc", "ig") #columns to loop through
#loop through combinations of targets and metrics and run prop.test
for (i in target_var) {
for (j in metric) {
d <- subset(test, target == i)
X <- d[,"tc"]
Y <- d[,j]
print(prop.test(table(X,Y),c(1,0),alternative="two.sided",
conf.level=0.95, correct=FALSE))
}
}
我不确定如何将所有 prop.test 运行的测试结果写入数据框。具体来说,每次测试运行我都需要 i、j、statistic、parameter、p.value、estimate、conf.int、null.value、alternative、method、data.name。
【问题讨论】:
-
查看
broom::tidy。
标签: r