```
# After nesting, you get one row per group
iris %>% nest_by(Species)
starwars %>% nest_by(species)
# The output is grouped by row, which makes modelling particularly easy
models <- mtcars %>%
nest_by(cyl) %>%
mutate(model = list(lm(mpg ~ wt, data = data)))
models
models %>% summarise(rsq = summary(model)$r.squared)
# This is particularly elegant with the broom functions
models %>% summarise(broom::glance(model))
models %>% summarise(broom::tidy(model))
# Note that you can also summarise to unnest the data
models %>% summarise(data)
```

Run the code above in your browser using DataLab