【发布时间】:2016-05-19 10:17:43
【问题描述】:
我正在尝试迭代约 60 列,目标是执行按案例/控制状态加权的 t 检验,并将输出捕获为列表。这是我迄今为止的尝试——请注意,我的数据框称为生物标记,第 3-59 列代表我感兴趣的变量,由第 2 列加权(称为案例):
tests <- list()
column_biomarkers <- colnames(biomarkers[3:59])
for (i in column_biomarkers){
tests[[i]] <- t.test(biomarkers$i[case == 1],biomarkers$i[case == 0],pool.sd=FALSE,na.rm=TRUE)
}
sapply(tests, function(x) {
c(x$estimate[1],
x$estimate[2],
ci.lower = x$conf.int[1],
ci.upper = x$conf.int[2],
p.value = x$p.value)
})
但是,我的这种尝试会导致以下错误:
var(x) 中的错误:'x' 为 NULL
任何建议将不胜感激!我是使用 R 的新手。
样本数据:
structure(list(subject = 1:10, case = c(1L, 0L, 0L, 1L, 1L, 0L,
0L, 0L, 0L, 1L), biomarker_1 = c(308.29999, 2533.3, 2723.3, 3125.3,
853, 6442.2998, 1472.5, 170.5, 64.5, 2624.8), biomarker_2 = c(4930.7998,
2401, 5158.5, 6526, 3774.2, 5753, 1955.2, 1332.2, 1296.8, 5859.2998
), biomarker_3 = c(4810, 3279.5, 7929.5, 8353, 4074.2, 7940.5,
1545.7, 2189.2, 1488.7, 6352.5)), .Names = c("subject", "case",
"biomarker_1", "biomarker_2", "biomarker_3"), row.names = c(NA,
10L), class = "data.frame")
【问题讨论】:
-
不知道为什么你有一个错误,但所有的
tests[[i]]应该是一样的,因为你没有在t.test()调用中使用i -
哎呀——应该是生物标志物$i ...抱歉,谢谢。我编辑了,上面是产生错误的代码。
-
你应该这样写:
biomarkers[case == 1,i] -
感谢 HubertL。如果我按照建议更正代码,则会收到以下错误: if (stderr
标签: r