commit 8c768c89ed752d24ef3ca41117f754140c290032
parent c6bf260c19a6aefd646ada8bb74ab92ea0d4b73b
Author: Jon Harmon <jonthegeek@gmail.com>
Date: Fri, 19 Aug 2022 07:30:13 -0500
Fix the TOC levels. (#20)
Diffstat:
1 file changed, 23 insertions(+), 20 deletions(-)
diff --git a/09_Functionals.Rmd b/09_Functionals.Rmd
@@ -1,6 +1,6 @@
# Functionals
-## **Learning objectives:** {-}
+**Learning objectives:** {-}
9.1. **Introduction**
@@ -52,7 +52,8 @@ out[[i]] <- f(x[[i]], ...)
out
}
```
-# **Benefit of using the map function in purrr** {-}
+
+## **Benefit of using the map function in purrr** {-}
- `purrr::map()` is equivalent to `lapply()`
@@ -68,7 +69,7 @@ out
library(tidyverse)
```
-# **Atomic vectors** {-}
+## **Atomic vectors** {-}
- has 4 variants to return atomic vectors
@@ -86,7 +87,7 @@ map_dbl(.x=1:3, .f=triple)
map_lgl(.x=c(1, NA, 3), .f=is.na)
```
-# Anonymous functions and shortcuts {-}
+## Anonymous functions and shortcuts {-}
**Anonymous functions**
```{r}
@@ -104,7 +105,9 @@ map_dbl(.x=mtcars, .f=~mean(.x, na.rm = TRUE))
```{r}
map_dbl(.x=mtcars, .f=mean, na.rm = TRUE)
```
+
## 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
```{r}
@@ -158,7 +161,7 @@ There are many variants
-# `map2_*()` {-}
+## `map2_*()` {-}
- raise each value `.x` by 2
@@ -179,7 +182,6 @@ map2_dbl(
)
```
----
## The benefit of using the map over apply family of function {-}
- It is written in C
@@ -189,7 +191,7 @@ map2_dbl(
- We can pass in some additional arguments to the function
-# `walk()` {-}
+## `walk()` {-}
- We use walk when we want to call a function for it side effect rather than it return value, like generating plots, `write.csv()` or `ggsave()`, `map()` will print more info than you may want
@@ -216,11 +218,11 @@ pwalk(.l=list(paths,plots),.f=ggsave,path=tempdir())
```
+
- walk, walk2 and pwalk all invisibly return .x the first argument. This makes them suitable for use in the middle of pipelines.
----
-# `imap()` {-}
+## `imap()` {-}
- `imap()` is like `map2()`except that `.y` is derived from `names(.x)` if named or `seq_along(.x)` if not
@@ -231,9 +233,8 @@ imap_chr(.x = mtcars, .f = ~ paste(.y, "has a mean of", round(mean(.x), 1))) %>%
head()
```
----
-# `pmap()` {-}
+## `pmap()` {-}
- you can pass a named list or dataframe as arguments to a function
@@ -279,7 +280,9 @@ Say you wanted to add up the numbers 1 through 5, but only using the plus operat
1 + 2 + 3 + 4 + 5
```
+
Which is the same thing as this:
+
```{r}
set.seed(1234)
@@ -296,7 +299,8 @@ identical(
)
```
-# ggplot2 Example with reduce {-}
+
+## ggplot2 Example with reduce {-}
```{r}
ggplot(mtcars, aes(hp, mpg)) +
@@ -305,7 +309,9 @@ ggplot(mtcars, aes(hp, mpg)) +
geom_point(size = 2, alpha = .5)
```
+
Let us use the reduce `function`
+
```{r}
reduce(
c(8, 4, 2),
@@ -333,9 +339,7 @@ accumulate(1:5, `+`)
```
----
-
-# Not covered: `map_df*()` variants {-}
+## Not covered: `map_df*()` variants {-}
- `map_dfr()` = row bind the results
@@ -356,7 +360,7 @@ map_dfr((1:2) * 10, col_stats)
---
-# Not covered: `pluck()` {-}
+## Not covered: `pluck()` {-}
- `pluck()` will pull a single element from a list
@@ -374,9 +378,8 @@ map(my_list, pluck, 1)
map_dbl(my_list, pluck, 1)
```
----
-# Not covered: `flatten()` {-}
+## Not covered: `flatten()` {-}
- `flatten()` will turn a list of lists into a simpler vector
@@ -394,6 +397,7 @@ map_if(my_list, is.list, flatten_int)
map_if(my_list, is.list, flatten_int) %>%
flatten_int()
```
+
## Dealing with Failures {-}
## Safely {-}
@@ -412,6 +416,7 @@ map(.x = A,.f = safely(log))
```
## Possibly {-}
+
Possibly always succeeds. It is simpler than safely, because you can give it a default value to return when there is an error.
```{r}
@@ -421,8 +426,6 @@ A <- list(1,10,"a")
```
----
-
## Meeting Videos