-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathbase.Rmd
More file actions
276 lines (207 loc) · 14.6 KB
/
base.Rmd
File metadata and controls
276 lines (207 loc) · 14.6 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
---
title: "purrr <-> base R"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{purrr <-> base R}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width = 7,
fig.height = 4.5,
fig.align = "center"
)
options(tibble.print_min = 6, tibble.print_max = 6)
```
# Introduction
This vignette compares purrr's functionals to their base R equivalents, focusing primarily on the map family and related functions.
This helps those familiar with base R understand better what purrr does, and shows purrr users how you might express the same ideas in base R code.
We'll start with a rough overview of the major differences, give a rough translation guide, and then show a few examples.
```{r setup}
library(purrr)
library(tibble)
```
## Key differences
There are two primary differences between the base apply family and the purrr map family: purrr functions are named more consistently, and more fully explore the space of input and output variants.
- purrr functions consistently use `.` as prefix to avoid [inadvertently matching arguments](https://adv-r.hadley.nz/functionals.html#argument-names) of the purrr function, instead of the function that you're trying to call.
Base functions use a variety of techniques including upper case (e.g. `lapply(X, FUN, ...)`) or require anonymous functions (e.g. `Map()`).
- All map functions are type stable: you can predict the type of the output using little information about the inputs.
In contrast, the base functions `sapply()` and `mapply()` automatically simplify making the return value hard to predict.
- The map functions all start with the data, followed by the function, then any additional constant argument.
Most base apply functions also follow this pattern, but `mapply()` starts with the function, and `Map()` has no way to supply additional constant arguments.
- purrr functions provide all combinations of input and output variants, and include variants specifically for the common two argument case.
## Direct translations
The following sections give a high-level translation between base R commands and their purrr equivalents.
See function documentation for the details.
### `Map` functions
Here `x` denotes a vector and `f` denotes a function
| Output | Input | Base R | purrr |
|------------------|------------------|------------------|-------------------|
| List | 1 vector | `lapply()` | `map()` |
| List | 2 vectors | `mapply()`, `Map()` | `map2()` |
| List | \>2 vectors | `mapply()`, `Map()` | `pmap()` |
| Atomic vector of desired type | 1 vector | `vapply()` | `map_lgl()` (logical), `map_int()` (integer), `map_dbl()` (double), `map_chr()` (character), `map_vec()` (other vectors) |
| Atomic vector of desired type | 2 vectors | `mapply()`, `Map()`, then `is.*()` to check type | `map2_lgl()` (logical), `map2_int()` (integer), `map2_dbl()` (double), `map2_chr()` (character), `map2_vec()` (other vectors) |
| Atomic vector of desired type | \>2 vectors | `mapply()`, `Map()`, then `is.*()` to check type | `pmap_lgl()` (logical), `pmap_int()` (integer), `pmap_dbl()` (double), `pmap_chr()` (character), `pmap_vec()` (other vectors) |
| Side effect only | 2 vectors | loops | `walk2()` |
| Side effect only | 1 vector | loops | `walk()` |
| Side effect only | \>2 vectors | loops | `pwalk()` |
| Data frame (`rbind` outputs) | 1 vector | `lapply()` then `rbind()` | `map()` then `list_rbind()` |
| Data frame (`rbind` outputs) | 2 vectors | `mapply()`/`Map()` then `rbind()` | `map2()` then `list_rbind()` |
| Data frame (`rbind` outputs) | \>2 vectors | `mapply()`/`Map()` then `rbind()` | `pmap()` then `list_rbind()` |
| Data frame (`cbind` outputs) | 1 vector | `lapply()` then `cbind()` | `map()` then `list_cbind()` |
| Data frame (`cbind` outputs) | 2 vectors | `mapply()`/`Map()` then `cbind()` | `map2()` then `list_cbind()` |
| Data frame (`cbind` outputs) | \>2 vectors | `mapply()`/`Map()` then `cbind()` | `pmap()` then `list_cbind()` |
| Any | Vector and its names | `l/s/vapply(X, function(x) f(x, names(x)))` or `mapply/Map(f, x, names(x))` | `imap()`, `imap_*()` (`lgl`, `dbl`, `chr`, and etc. just like for `map()`, `map2()`, and `pmap()`) |
| Any | Selected elements of the vector | `l/s/vapply(X[index], FUN, ...)` | `map_if()`, `map_at()` |
| List | Recursively apply to list within list | `rapply()` | `map_depth()` |
| List | List only | `lapply()` | `lmap()`, `lmap_at()`, `lmap_if()` |
### Extractor shorthands
Since a common use case for map functions is list extracting components, purrr provides a handful of shortcut functions for various uses of `[[`.
| Input | base R | purrr |
|-------------------|--------------------------|---------------------------|
| Extract by name | `` lapply(x, `[[`, "a") `` | `map(x, "a")` |
| Extract by position | `` lapply(x, `[[`, 3) `` | `map(x, 3)` |
| Extract deeply | `lapply(x, \(y) y[[1]][["x"]][[3]])` | `map(x, list(1, "x", 3))` |
| Extract with default value | `lapply(x, function(y) tryCatch(y[[3]], error = function(e) NA))` | `map(x, 3, .default = NA)` |
### Predicates
Here `p`, a predicate, denotes a function that returns `TRUE` or `FALSE` indicating whether an object fulfills a criterion, e.g. `is.character()`.
| Description | base R | purrr |
|-----------------------------|--------------------|-----------------------|
| Find a matching element | `Find(p, x)` | `detect(x, p)`, |
| Find position of matching element | `Position(p, x)` | `detect_index(x, p)` |
| Do all elements of a vector satisfy a predicate? | `all(sapply(x, p))` | `every(x, p)` |
| Does any elements of a vector satisfy a predicate? | `any(sapply(x, p))` | `some(x, p)` |
| Does a list contain an object? | `any(sapply(x, identical, obj))` | `has_element(x, obj)` |
| Keep elements that satisfy a predicate | `x[sapply(x, p)]` | `keep(x, p)` |
| Discard elements that satisfy a predicate | `x[!sapply(x, p)]` | `discard(x, p)` |
| Negate a predicate function | `function(x) !p(x)` | `negate(p)` |
### Other vector transforms
| Description | base R | purrr |
|-----------------------------|--------------------|-----------------------|
| Accumulate intermediate results of a vector reduction | `Reduce(f, x, accumulate = TRUE)` | `accumulate(x, f)` |
| Recursively combine two lists | `c(X, Y)`, but more complicated to merge recursively | `list_merge()`, `list_modify()` |
| Reduce a list to a single value by iteratively applying a binary function | `Reduce(f, x)` | `reduce(x, f)` |
## Examples
### Varying inputs
#### One input
Suppose we would like to generate a list of samples of 5 from normal distributions with different means:
```{r}
means <- 1:4
```
There's little difference when generating the samples:
- Base R uses `lapply()`:
```{r}
set.seed(2020)
samples <- lapply(means, rnorm, n = 5, sd = 1)
str(samples)
```
- purrr uses `map()`:
```{r}
set.seed(2020)
samples <- map(means, rnorm, n = 5, sd = 1)
str(samples)
```
#### Two inputs
Lets make the example a little more complicated by also varying the standard deviations:
```{r}
means <- 1:4
sds <- 1:4
```
- This is relatively tricky in base R because we have to adjust a number of `mapply()`'s defaults.
```{r}
set.seed(2020)
samples <- mapply(
rnorm,
mean = means,
sd = sds,
MoreArgs = list(n = 5),
SIMPLIFY = FALSE
)
str(samples)
```
Alternatively, we could use `Map()` which doesn't simplify, but also doesn't take any constant arguments, so we need to use an anonymous function:
```{r}
samples <- Map(function(...) rnorm(..., n = 5), mean = means, sd = sds)
```
In R 4.1 and up, you could use the shorter anonymous function form:
```{r}
samples <- Map(\(...) rnorm(..., n = 5), mean = means, sd = sds)
```
- Working with a pair of vectors is a common situation so purrr provides the `map2()` family of functions:
```{r}
set.seed(2020)
samples <- map2(means, sds, rnorm, n = 5)
str(samples)
```
#### Any number of inputs
We can make the challenge still more complex by also varying the number of samples:
```{r}
ns <- 4:1
```
- Using base R's `Map()` becomes more straightforward because there are no constant arguments.
```{r}
set.seed(2020)
samples <- Map(rnorm, mean = means, sd = sds, n = ns)
str(samples)
```
- In purrr, we need to switch from `map2()` to `pmap()` which takes a list of any number of arguments.
```{r}
set.seed(2020)
samples <- pmap(list(mean = means, sd = sds, n = ns), rnorm)
str(samples)
```
### Outputs
Given the samples, imagine we want to compute their means.
A mean is a single number, so we want the output to be a numeric vector rather than a list.
- There are two options in base R: `vapply()` or `sapply()`.
`vapply()` requires you to specific the output type (so is relatively verbose), but will always return a numeric vector.
`sapply()` is concise, but if you supply an empty list you'll get a list instead of a numeric vector.
```{r}
# type stable
medians <- vapply(samples, median, FUN.VALUE = numeric(1L))
medians
# not type stable
medians <- sapply(samples, median)
```
- purrr is little more compact because we can use `map_dbl()`.
```{r}
medians <- map_dbl(samples, median)
medians
```
What if we want just the side effect, such as a plot or a file output, but not the returned values?
- In base R we can either use a for loop or hide the results of `lapply`.
```{r, fig.show='hide'}
# for loop
for (s in samples) {
hist(s, xlab = "value", main = "")
}
# lapply
invisible(lapply(samples, function(s) {
hist(s, xlab = "value", main = "")
}))
```
- In purrr, we can use `walk()`.
```{r, fig.show='hide'}
walk(samples, ~ hist(.x, xlab = "value", main = ""))
```
### Pipes
You can join multiple steps together with the pipe:
```{r}
set.seed(2020)
means |>
map(rnorm, n = 5, sd = 1) |>
map_dbl(median)
```
The pipe is particularly compelling when working with longer transformations.
For example, the following code splits `mtcars` up by `cyl`, fits a linear model, extracts the coefficients, and extracts the first one (the intercept).
```{r}
mtcars |>
split(mtcars$cyl) |>
map(\(df) lm(mpg ~ wt, data = df))|>
map(coef) |>
map_dbl(1)
```