【发布时间】:2018-06-08 20:13:21
【问题描述】:
我有 26 个比例和 26 个样本大小。像这样:
## rates for 26 regions
crude2admit_rate <- c(0.18715084,0.00000000, 0.11111111, 0.03333333, 0.17500000, 0.10810811, 0.10080645, 0.14388489, 0.19029374, 0.15268456, 0.18329278,
0.10614525, 0.06896552, 0.25000000, 0.10294118, 0.21000981, 0.16950998, 0.18333333, 0.14355231, 0.26595745, 0.18317890, 0.13636364, 0.20556227, 0.21547800 ,0.20924574, 0.23748669)
## N for 26 regions
count_pat <- c(358,7,18,30,40,37,496,973,4698,596,1233,179,29,12,68,1019,2755,60,411,94,1403,352,827,659,411,939)
我编写了一个循环来对 26 个区域中的每一个运行二项式检验,如下所示:
for (i in 1:26) {
ci[[i]] <- binom.test(x = round(crude2admit_rate[i]*count_pat[i]), n = count_pat[i])
}
我想从每个测试中提取 26 个置信区间,如下所示:
## 1st region
ci[[1]]$conf.int[1] #lower bounds
ci[[1]]$conf.int[2] #upper bounds
## 2nd region
ci[[2]]$conf.int[1]
ci[[2]]$conf.int[2]
如何编写loop 来提取 26 个上下限,并将它们保存为列表或数据框?
谢谢!!
【问题讨论】:
-
lapply(ci, "[", "conf.int")将拉出每个列表的conf.int元素。 -
你可以去掉多余的属性并将它们全部放在一个矩阵中
sapply(ci, function(x) unclass(x$conf.int)) -
知道了,
sapply非常适合我。谢谢!