Update iteration.qmd (#1071)

Provided another way of getting R squared by using `nest()` from tidyr, which makes the code to be tidyverse consistent.
This commit is contained in:
Y. Yu 2022-08-16 12:37:44 -04:00 committed by GitHub
parent 5ac3dac6bd
commit f3db882d9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 1 deletions

View File

@ -612,6 +612,18 @@ models |>
map_dbl("r.squared")
```
Another way to obtain R squared is by using the broom package. Instead of using `split()` from base R, you can use `nest()` from tidyr:
```{r}
mtcars |>
nest(data = -cyl) |>
arrange(cyl) |>
mutate(mod = map(data, ~lm(mpg ~ wt, data = .)),
glanced = map(mod, broom::glance)) |>
unnest(glanced) %>%
pull(r.squared)
```
You can also use an integer to select elements by position:
```{r}
@ -705,7 +717,7 @@ str(safe_log("a"))
When the function succeeds, the `result` element contains the result and the `error` element is `NULL`.
When the function fails, the `result` element is `NULL` and the `error` element contains an error object.
`safely()` is designed to work with map:
`safely()` is designed to work with `map()`:
```{r}
x <- list(1, 10, "a")