[https://speakerdeck.com/romainfrancois/n-cool-number-dplyr-things]

group_hug()

Split data in groups

Apply something for each group

Combine

group_modify()

    library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
  fun <- function(slice, keys) {
      broom::tidy(lm(Petal.Length ~ Sepal.Length, data = slice))
  }
  
  iris %>% 
      group_by(Species) %>%
      group_modify(fun)
## # A tibble: 6 x 6
## # Groups:   Species [3]
##   Species    term         estimate std.error statistic  p.value
##   <fct>      <chr>           <dbl>     <dbl>     <dbl>    <dbl>
## 1 setosa     (Intercept)     0.803    0.344      2.34  2.38e- 2
## 2 setosa     Sepal.Length    0.132    0.0685     1.92  6.07e- 2
## 3 versicolor (Intercept)     0.185    0.514      0.360 7.20e- 1
## 4 versicolor Sepal.Length    0.686    0.0863     7.95  2.59e-10
## 5 virginica  (Intercept)     0.610    0.417      1.46  1.50e- 1
## 6 virginica  Sepal.Length    0.750    0.0630    11.9   6.30e-16
iris %>% 
    group_by(Species) %>%
    group_modify(
        ~ broom::tidy(lm(Petal.Length ~ Sepal.Length, data = .x))
    )
## # A tibble: 6 x 6
## # Groups:   Species [3]
##   Species    term         estimate std.error statistic  p.value
##   <fct>      <chr>           <dbl>     <dbl>     <dbl>    <dbl>
## 1 setosa     (Intercept)     0.803    0.344      2.34  2.38e- 2
## 2 setosa     Sepal.Length    0.132    0.0685     1.92  6.07e- 2
## 3 versicolor (Intercept)     0.185    0.514      0.360 7.20e- 1
## 4 versicolor Sepal.Length    0.686    0.0863     7.95  2.59e-10
## 5 virginica  (Intercept)     0.610    0.417      1.46  1.50e- 1
## 6 virginica  Sepal.Length    0.750    0.0630    11.9   6.30e-16

group_map

iris %>%
    group_by(Species) %>%
    group_map( ~ lm(Petal.Length ~ Sepal.Length, data = .x))
## [[1]]
## 
## Call:
## lm(formula = Petal.Length ~ Sepal.Length, data = .x)
## 
## Coefficients:
##  (Intercept)  Sepal.Length  
##       0.8031        0.1316  
## 
## 
## [[2]]
## 
## Call:
## lm(formula = Petal.Length ~ Sepal.Length, data = .x)
## 
## Coefficients:
##  (Intercept)  Sepal.Length  
##       0.1851        0.6865  
## 
## 
## [[3]]
## 
## Call:
## lm(formula = Petal.Length ~ Sepal.Length, data = .x)
## 
## Coefficients:
##  (Intercept)  Sepal.Length  
##       0.6105        0.7501

group_modify() diy with group_map()

iris %>%
    group_by(Species) %>%
    group_map( ~ {
        broom::tidy(lm(Petal.Length ~ Sepal.Length, data = .x)) %>%
            tibble::add_column(Species = .y$Species)
    }) %>%
    bind_rows() %>%
    group_by(Species)
## # A tibble: 6 x 6
## # Groups:   Species [3]
##   term         estimate std.error statistic  p.value Species   
##   <chr>           <dbl>     <dbl>     <dbl>    <dbl> <fct>     
## 1 (Intercept)     0.803    0.344      2.34  2.38e- 2 setosa    
## 2 Sepal.Length    0.132    0.0685     1.92  6.07e- 2 setosa    
## 3 (Intercept)     0.185    0.514      0.360 7.20e- 1 versicolor
## 4 Sepal.Length    0.686    0.0863     7.95  2.59e-10 versicolor
## 5 (Intercept)     0.610    0.417      1.46  1.50e- 1 virginica 
## 6 Sepal.Length    0.750    0.0630    11.9   6.30e-16 virginica
iris %>%
    group_by(Species) %>%
    group_map( ~{
        broom::tidy(lm(Petal.Length ~ Sepal.Length, data = .x)) %>%
            tibble::add_column(!!!.y)
    }) %>%
    bind_rows() %>%
    group_by(Species)
## # A tibble: 6 x 6
## # Groups:   Species [3]
##   term         estimate std.error statistic  p.value Species   
##   <chr>           <dbl>     <dbl>     <dbl>    <dbl> <fct>     
## 1 (Intercept)     0.803    0.344      2.34  2.38e- 2 setosa    
## 2 Sepal.Length    0.132    0.0685     1.92  6.07e- 2 setosa    
## 3 (Intercept)     0.185    0.514      0.360 7.20e- 1 versicolor
## 4 Sepal.Length    0.686    0.0863     7.95  2.59e-10 versicolor
## 5 (Intercept)     0.610    0.417      1.46  1.50e- 1 virginica 
## 6 Sepal.Length    0.750    0.0630    11.9   6.30e-16 virginica

group_split()