Skip to content

Commit

Permalink
Jo chp 9 (#66)
Browse files Browse the repository at this point in the history
* edits to chapter 9 for Friday's cohort 9 presentation

* a few extra changes post-bookclub

---------

Co-authored-by: Jon Harmon <[email protected]>
  • Loading branch information
hardin47 and jonthegeek authored Jul 28, 2024
1 parent e1e92bd commit bca1862
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions 09_Functionals.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ Note that `.x` is the **name** of the first argument in `map()` (`.f` is the nam

```{r}
#| error: true
# the task
map_dbl(mtcars, function(x) length(unique(x)))
map_dbl(mtcars, function(unicorn) length(unique(unicorn)))
map_dbl(mtcars, ~length(unique(.x)))
map_dbl(mtcars, ~length(unique(..1)))
map_dbl(mtcars, ~length(unique(.)))
Expand All @@ -131,16 +131,23 @@ map_dbl(mtcars, ~length(unique(.)))
map_dbl(mtcars, length)
map_dbl(mtcars, length(unique))
map_dbl(mtcars, 1)
```

```{r}
#| echo: false
#| message: false
#| warning: false
rm(x)
```

```{r}
#| error: true
#error
map_dbl(mtcars, length(unique()))
map_dbl(mtcars, ~length(unique(x)))
```



## Modify {-}

Sometimes we might want the output to be the same as the input, then in that case we can use the modify function rather than map
Expand Down Expand Up @@ -202,8 +209,6 @@ There are many variants
![](images/map_variants.png)




## `map2_*()` {-}

- raise each value `.x` by 2
Expand Down Expand Up @@ -249,6 +254,14 @@ map(1:3, ~cat(.x, "\n"))
walk(1:3, ~cat(.x, "\n"))
```

`cat()` does have a result, it's just usually returned invisibly.

```{r}
cat("hello")
(cat("hello"))
```


We can use `pwalk()` to save a list of plot to disk. Note that the "p" in `pwalk()` means that we have more than 1 (or 2) variables to pipe into the function. Also note that the name of the first argument in all of the "p" functions is now `.l` (instead of `.x`).

Expand Down Expand Up @@ -340,6 +353,8 @@ The `reduce()` function is a powerful functional that allows you to abstract awa

`reduce()` takes a vector as its first argument, a function as its second argument, and an optional `.init` argument last. It will then apply the function repeatedly to the vector until there is only a single element left.

(Hint: start at the top of the image and read down.)

```{r,echo=FALSE,warning=FALSE,message=FALSE}
knitr::include_graphics(path = 'images/reduce-init.png')
```
Expand Down Expand Up @@ -422,7 +437,7 @@ accumulate2(
```


## Not covered: `map_df*()` variants {-}
## `map_df*()` variants {-}

- `map_dfr()` = row bind the results

Expand All @@ -447,7 +462,7 @@ map((1:2) * 10, col_stats) |> list_rbind()

---

## Not covered: `pluck()` {-}
## `pluck()` {-}

- `pluck()` will pull a single element from a list

Expand All @@ -472,7 +487,7 @@ map(my_list, pluck, 1)
map_dbl(my_list, pluck, 1)
```

The `map()` functions also have shortcuts for extracting elements from vectors (powered by `purrr::pluck()`).
The `map()` functions also have shortcuts for extracting elements from vectors (powered by `purrr::pluck()`). Note that `map(my_list, 3)` is a shortcut for `map(my_list, pluck, 3)`.

```{r}
#| error: true
Expand Down Expand Up @@ -501,7 +516,6 @@ map_chr(my_list, "z", .default = NA)
```



## Not covered: `flatten()` {-}

- `flatten()` will turn a list of lists into a simpler vector.
Expand Down

0 comments on commit bca1862

Please sign in to comment.