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.
- 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
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
See Question&Answers more detail:os