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
dplyr? With,data.tableyou can quickly dosetDT(dd)[, as.list(get_binCI(x, n)), by = .(x, n)]Though my mind reading skills are not allowing me to determine what do you exactly mean by "expressive way"... - David Arenburgdplyranswer (although I will not be surprised if my solution above is the best one can do ATM). I have nothing againstdata.table, but I preferdplyr, and -- mostly -- I'm still spending a lot of brainpower getting my head around it, don't really want to add a whole new set of syntax (nor inflict it on my students and colleagues) at the moment. But if you answer that way I'll upvote, it's useful. - Ben Bolkerunnestthat also usesmap2that you might be interested in - markdly