- library(dplyr)
- library(tidyr)
- temp <- data.frame(a = c(1,2) , b = c(2,4), type = c('A','B'))
- fun <- function(a, b)
- {
- b <- a + b
- return(data.frame(a,b))
- }
- temp %>% group_by(type) %>%
- summarise(result = list(fun(a, b))) %>% unnest(result)
- # # A tibble: 2 × 3
- # type a b
- # <fctr> <dbl> <dbl>
- # A 2 3
- # B 2 6
- temp %>% group_by(type) %>%
- do(result = fun(.$a,.$b)) %>% unnest(result)
- # # A tibble: 2 × 3
- # type a b
- # <fctr> <dbl> <dbl>
- # A 1 3
- # B 2 6
两者结果不同,其中明显do是对的结果。应该如何理解summarise的作用机理?


雷达卡


京公网安备 11010802022788号







