dplyr::mutate to add multiple values

前端 未结 6 1590
旧时难觅i
旧时难觅i 2020-11-30 04:15

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.

6条回答
  •  爱一瞬间的悲伤
    2020-11-30 04:36

    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.

提交回复
热议问题