用dplyr调用R中的prop.test函数 [英] Calling prop.test function in R with dplyr
问题描述
我正在尝试计算几个二项式比例置信区间。我的数据在数据框中,尽管我可以从 prop.test
返回的对象中成功提取估计
, conf.int
变量在数据帧上运行时似乎为空。
I am trying to calculate several binomial proportion confidence intervals. My data are in a data frame, and though I can successfully extract the estimate
from the object returned by prop.test
, the conf.int
variable seems to be null when run on the data frame.
library(dplyr)
cases <- c(50000, 1000, 10, 2343242)
population <- c(100000000, 500000000, 100000, 200000000)
df <- as.data.frame(cbind(cases, population))
df %>% mutate(rate = prop.test(cases, population, conf.level=0.95)$estimate)
这将适当地返回
cases population rate
1 50000 1e+08 0.00050000
2 1000 5e+08 0.00000200
3 10 1e+05 0.00010000
4 2343242 2e+08 0.01171621
但是,当我跑步时
df %>% mutate(confint.lower= prop.test(cases, pop, conf.level=0.95)$conf.int[1])
我很难过ly get
I sadly get
Error in mutate_impl(.data, dots) :
Column `confint.lower` is of unsupported type NULL
有什么想法吗?我知道计算二项式比例置信区间的其他方法,但我真的很想学习如何很好地使用 dplyr
。
Any thoughts? I know alternative ways to calculate the binomial proportion confidence interval, but I would really like to learn how to use dplyr
well.
谢谢!
推荐答案
您可以使用 dplyr :: rowwise()
按行分组:
df %>%
rowwise() %>%
mutate(lower_ci = prop.test(cases, pop, conf.level=0.95)$conf.int[1])
默认情况下, dplyr
接受列名并将其视为向量。因此,像上面提到的@Jake Fisher一样,矢量化函数无需添加 rowwise()
即可工作。
By default dplyr
takes the column names and treats them like vectors. So vectorized functions, like @Jake Fisher mentioned above, just work without rowwise()
added.
这就是我会立即捕获所有置信区间成分:
This is what I would do to catch all of the confidence interval components at once:
df %>%
rowwise %>%
mutate(tst = list(broom::tidy(prop.test(cases, pop, conf.level=0.95)))) %>%
tidyr::unnest(tst)
这篇关于用dplyr调用R中的prop.test函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!