dplyr :: mutate添加多个值 [英] dplyr::mutate to add multiple values

查看:270
本文介绍了dplyr :: mutate添加多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经有 dplyr Github repo 上有几个问题,至少有一个相关的SO问题,但都没有涵盖我的问题 - 我想。

There are a couple of issues about this on the dplyr Github repo already, and at least one related SO question, but none of them quite covers my question -- I think.


  • 在dplyr mutate调用中添加多个列或多或少是我想要的,但是有一个特殊情况的答案( tidyr :: separate )(我认为)对我有用。

  • 此问题(使用返回多个值/列的函数进行汇总或变异)表示使用 do()

  • Adding multiple columns in a dplyr mutate call is more or less what I want, but there's a special-case answer for that case (tidyr::separate) that doesn't (I think) work for me.
  • This issue ("summarise or mutate with functions returning multiple values/columns") says "use do()".

这是我的用例:我想计算精确的二项置信区间

Here's my use case: I want to compute exact binomial confidence intervals

dd <- data.frame(x=c(3,4),n=c(10,11))
get_binCI <- function(x,n) {
    rbind(setNames(c(binom.test(x,n)$conf.int),c("lwr","upr")))
}
with(dd[1,],get_binCI(x,n))
##             lwr       upr
## [1,] 0.06673951 0.6524529

我可以使用 do()完成这项工作,但我想知道是否有更有表现力的方法来做到这一点(感觉像 mutate () 可以有一个 .n 参数正在讨论summaryize() ...)

I can get this done with do() but I wonder if there's a more expressive way to do this (it feels like mutate() could have a .n argument as is being discussed for summarise() ...)

library("dplyr")
dd %>% group_by(x,n) %>%
    do(cbind(.,get_binCI(.$x,.$n)))

## Source: local data frame [2 x 4]
## Groups: x, n
## 
##   x  n        lwr       upr
## 1 3 10 0.06673951 0.6524529
## 2 4 11 0.10926344 0.6920953


推荐答案

另一种变体,虽然我认为我们都在这里分裂头发。

Yet another variant, although I think we're all splitting hairs here.

> dd <- data.frame(x=c(3,4),n=c(10,11))
> get_binCI <- function(x,n) {
+   as_data_frame(setNames(as.list(binom.test(x,n)$conf.int),c("lwr","upr")))
+ }
> 
> dd %>% 
+   group_by(x,n) %>%
+   do(get_binCI(.$x,.$n))
Source: local data frame [2 x 4]
Groups: x, n

  x  n        lwr       upr
1 3 10 0.06673951 0.6524529
2 4 11 0.10926344 0.6920953

个人而言,如果我们只是通过可读性,我觉得这更可取:

Personally, if we're just going by readability, I find this preferable:

foo  <- function(x,n){
    bi <- binom.test(x,n)$conf.int
    data_frame(lwr = bi[1],
               upr = bi[2])
}

dd %>% 
    group_by(x,n) %>%
    do(foo(.$x,.$n))

...但现在我们是真的分裂头发。

...but now we're really splitting hairs.

这篇关于dplyr :: mutate添加多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆