我们可以用'prop.test'的参数改变列名后使用pmap
pmap(setNames(df, c("x", "n")), prop.test)
或使用map2
map2(df$Success, df$N, prop.test)
map 的问题在于它循环遍历数据集的每一列,它是vectors 中的list
df %>%
map(~ .x)
#$Success
#[1] 38 12 27 9
#$N
#[1] 50 50 50 50
所以,我们不能做.x$Success 或.x$N
更新
正如@Steven Beaupre 所说,如果我们需要创建具有 p 值和置信区间的新列
res <- df %>%
mutate(newcol = map2(Success, N, prop.test),
pval = map_dbl(newcol, ~ .x[["p.value"]]),
CI = map(newcol, ~ as.numeric(.x[["conf.int"]]))) %>%
select(-newcol)
# A tibble: 4 x 4
# Success N pval CI
# <dbl> <dbl> <dbl> <list>
#1 38.0 50.0 0.000407 <dbl [2]>
#2 12.0 50.0 0.000407 <dbl [2]>
#3 27.0 50.0 0.671 <dbl [2]>
#4 9.00 50.0 0.0000116 <dbl [2]>
“CI”列是由 2 个元素组成的 list,可以通过 unnested 使其成为“长”格式数据
res %>%
unnest
或者创建 3 列
df %>%
mutate(newcol = map2(Success, N, ~ prop.test(.x, n = .y) %>%
{tibble(pvalue = .[["p.value"]],
CI_lower = .[["conf.int"]][[1]],
CI_upper = .[["conf.int"]][[2]])})) %>%
unnest
# A tibble: 4 x 5
# Success N pvalue CI_lower CI_upper
# <dbl> <dbl> <dbl> <dbl> <dbl>
#1 38.0 50.0 0.000407 0.615 0.865
#2 12.0 50.0 0.000407 0.135 0.385
#3 27.0 50.0 0.671 0.395 0.679
#4 9.00 50.0 0.0000116 0.0905 0.319