-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter_21.Rmd
49 lines (33 loc) · 1.06 KB
/
Chapter_21.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
title: "Chapter 21"
author: "Laura"
date: "12/15/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
library(tidyverse); library(skimr); library(nycflights13); library(GGally); library(ggstance); library(lvplot); library(hexbin); library(modelr); library(magrittr); library(stringr); library(htmlwidgets); library(forcats); library(lubridate); #library(maps); library(mapproj);
```
## Notes for Chapter 21: Iteration
```{r}
mtcars %>%
split(.$cyl) %>%
map(~ lm(mpg ~ wt, data = .x))
map(1:5, runif)
# from primer
nested_gapminder <- gapminder %>%
group_by(country) %>%
nest()
nested_gapminder %>%
pluck("data") %>%
pluck(1)
nested_gapminder %>%
pluck("data") %>%
map(~ lm(lifeExp ~ year, data = .))
gap_models <- nested_gapminder %>%
mutate(models = map(data, ~ lm(lifeExp ~ year, data = .x)))
gap_coefs <- gap_models %>%
mutate(coefficient = map_dbl(models, ~ coef(.x) %>% pluck("year")))
gap_coefs %>%
mutate(r_squared = map_dbl(models, ~ summary(.x) %>% pluck("r.squared")))
```