-
Notifications
You must be signed in to change notification settings - Fork 4
/
03-iteration-01.qmd
585 lines (383 loc) · 9.94 KB
/
03-iteration-01.qmd
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
---
title: "useR to programmeR"
subtitle: "Iteration 1"
author: "Emma Rand and Ian Lyttle"
format:
revealjs:
theme: [simple, styles.scss]
footer: <https://pos.it/programming_r_24>
slide-number: true
chalkboard: true
code-link: true
code-line-numbers: false
bibliography: references.bib
---
# Overview
## Overview
In this session we will cover another way to reduce code duplication: iteration.
## Learning Objectives
At the end of this section you will be able to:
::: {style="font-size: 80%;"}
- recognise that much iteration comes free with R
- iterate across rows using `across()`
- use selection functions to select columns for iteration
- use anonymous functions to pass arguments
- give more than one function for iteration
- use `.names` to control the output
- use `across()` in functions
:::
## What is iteration?
- Iteration means repeating steps multiple times until a condition is met
- In other languages, iteration is performed with loops: `for`, `while`
. . .
- Iteration is different in R
- You *can* use loops....... but you often don't *need* to
## Iteration in R
Iteration is an inherent part of the language. For example, if
```{r}
nums <- c(3, 1, 6, 4)
```
Then
```{r}
#| eval: false
2 * nums
```
is
## Iteration in R
``` r
[1] 6 2 12 8
```
and NOT
. . .
``` r
[1] 6 2 12 8 6 2 12 8
```
## Iteration in R
We have:
- `group_by()` with `summarize()`
- `facet_wrap()`
- `across()` and `purrr()`
. . . - the `apply()` family
. . .
other languages, a for loop would be right after hello world
## Functional programming
"functional programming" because functions take other functions as input
- modifying multiple columns {dplyr}
- reading multiple files {purrr}
- saving multiple outputs {purrr}
# Set up
## Create a `.R`
```{r}
#| eval: false
usethis::use_r("iteration-01")
```
## Packages
🎬 Load packages:
```{r}
library(tidyverse)
library(palmerpenguins)
```
## Load `penguins`
🎬 Load `penguins` data set
```{r}
data(penguins)
glimpse(penguins)
```
# Modifying multiple columns
## Scenario
Recall our standard error function from this morning:
```{r}
sd_error <- function(x){
sd(x, na.rm = TRUE) / sqrt(sum(!is.na(x)))
}
```
## Scenario
Which we might use as:
```{r}
penguins |>
summarise(se_bill_len = sd_error(bill_length_mm),
se_bill_dep = sd_error(bill_depth_mm),
se_flip_len = sd_error(flipper_length_mm ),
se_body_mas = sd_error(body_mass_g))
```
. . .
⚠️ Code repetition!
How can we iterate over rows?
## Solution: `across()`
```{r}
penguins |>
summarise(across(bill_length_mm:body_mass_g, sd_error))
```
## `across()` Arguments
`across(.cols, .fns, .names)`
3 important arguments
## `across()` Arguments
- which columns you want to iterate over: `.cols = bill_length_mm:body_mass_g`
. . .
- what you want to do to each column: `.fns = sd_error`
- single function, include arguments, more than one function
. . .
- `.names` to control output
## selecting columns with `.cols`
- we could use colon notation, `bill_length_mm:body_mass_g`, because columns are adjacent
. . .
but
- `.cols` uses same specification as `select()`: `starts_with()`, `ends_with()`, `contains()`, `matches()`
## selecting columns with `.cols`
```{r}
penguins |>
summarise(across(ends_with("mm"), sd_error))
```
## selecting columns with `.cols`
- `everything()`: all non-grouping columns
```{r}
penguins |>
group_by(species, island, sex) |>
summarise(across(everything(), sd_error))
```
## selecting columns with `.cols`
```{r}
#| eval: false
penguins |>
group_by(species, island, sex) |>
summarise(across(everything(), sd_error))
```
- variables in `group_by()` are excluded
- all of `bill_length_mm`, `bill_depth_mm`, `flipper_length_mm`, `body_mass_g`, `year`
## selecting columns with `.cols`
- `everything()`: all non-grouping columns without year
```{r}
penguins |>
select(-year) |>
group_by(species, island, sex) |>
summarise(across(everything(), sd_error))
```
## selecting columns with `.cols`
- My columns have very different names and I don't want to group!
. . .
- all the *numeric* columns: `where()`
```{r}
penguins |>
select(-year) |>
summarise(across(where(is.numeric), sd_error))
```
## `.funs`: calling one function
- we can pass a function, `sd_error` to `across()` since R is a functional programming language
- note, we are not calling `sd_error()`
- instead we pass `sd_error` so `across()` can call it
- thus function name is **not** followed by `()`
## function name is **not** followed by `()`
📢
```{r}
#| error: true
penguins |>
select(-year) |>
summarise(across(where(is.numeric), sd_error()))
```
. . .
This error is easy to make!
## Include arguments
```{r}
penguins |>
summarise(across(ends_with("mm"), mean))
```
We get the NA because we have missing values[^1].
[^1]: There is no problem when we use `sd_error()` because we accounted for NA in our function definition
## Include arguments
`mean()` has an `na.rm` argument.
How can we pass on `na.rm = TRUE`?
. . .
We might try:
```{r}
#| error: true
penguins |>
summarise(across(ends_with("mm"), mean(na.rm = TRUE)))
```
## Include arguments
The solution is to create a new function that calls `mean()` with `na.rm = TRUE`
. . .
```{r}
penguins |>
summarise(across(ends_with("mm"),
function(x) mean(x, na.rm = TRUE)))
```
. . .
`mean` is replaced by a function definition
## Anonymous functions
``` r
penguins |>
summarise(across(ends_with("mm"),
function(x) mean(x, na.rm = TRUE)))
```
- This is called an **anonymous** or **lambda** function.
- It is anonymous because we do not give it a name with `<-`
## Anonymous functions
Shorthand
. . .
Instead of writing `function` we can use `\`
```{r}
penguins |>
summarise(across(ends_with("mm"),
\(x) mean(x, na.rm = TRUE)))
```
## Anonymous functions
Note, You might also see:
```{r}
penguins |>
summarise(across(ends_with("mm"),
~ mean(.x, na.rm = TRUE)))
```
. . .
- `\(x)` is base syntax new in 4.1.0 **Recommended**
- `~ .x` is fine but only works in tidyverse functions
## `.funs`: calling more than one function
How can we use more than one function across the columns?
``` r
penguins |>
summarise(across(ends_with("mm"), _MORE THAN ONE FUNCTION_))
```
. . .
by using a list
## `.funs`: calling more than one function
Using a list:
``` r
penguins |>
summarise(across(where(is.numeric), list(
sd_error,
length)))
```
. . .
Or, with anonymous functions:
``` r
penguins |>
summarise(across(ends_with("mm"), list(
\(x) mean(x, na.rm = TRUE),
\(x) sd(x, na.rm = TRUE))))
```
## `.funs`: calling more than one function
```{r}
penguins |>
summarise(across(ends_with("mm"), list(
\(x) mean(x, na.rm = TRUE),
\(x) sd(x, na.rm = TRUE))))
```
. . .
Problem: the suffixes `_1` and `_2` for functions are not very useful.
## `.funs`: calling more than one function
We can improve by naming the elements in the list
```{r}
penguins |>
summarise(across(ends_with("mm"), list(
mean = \(x) mean(x, na.rm = TRUE),
sdev = \(x) sd(x, na.rm = TRUE))))
```
. . .
The column name is `{.col}_{.fn}`: `bill_length_mm_mean`
fn: **f**unction **n**ame
. . .
We can change using the `.names` argument
## `.names` to control output
```{r}
penguins |>
summarise(across(ends_with("mm"),
list(mean = \(x) mean(x, na.rm = TRUE),
sdev = \(x) sd(x, na.rm = TRUE)),
.names = "{.fn}_of_{.col}"))
```
## `.names` to control output
Especially important for `mutate()`.
Recall our `to_z()` function
```{r}
to_z <- function(x, middle = 1) {
trim = (1 - middle)/2
(x - mean(x, na.rm = TRUE, trim = trim)) / sd(x, na.rm = TRUE)
}
```
## `to_z()` function in `mutate()`
which we used like this
```{r}
penguins |>
mutate(
z_bill_length_mm = to_z(bill_length_mm),
z_bill_depth_mm = to_z(bill_depth_mm),
z_flipper_length_mm = to_z(flipper_length_mm)
) |>
glimpse()
```
## `.names` to control output
It makes sense to use `across()` to apply the transformation to all three variables
```{r}
penguins |>
mutate(across(ends_with("mm"),
to_z)
) |>
glimpse()
```
😮 Results go into existing columns!
##
```{r}
penguins |>
mutate(across(ends_with("mm"),
to_z,
.names = "z_{.col}")
) |>
glimpse()
```
<!-- ## A note on dots in argument names -->
<!-- - -->
<!-- - -->
<!-- ## Iteration over columns in `filter()` -->
<!-- ?? -->
## Your turn
Time to bring together functions and iteration!
🎬 Write a function that summarises multiple specified columns of a data frame
``` r
my_summary <- function(df, cols) {
. . . .
}
```
``` r
my_summary(penguins, ends_with("mm"))
```
## A solution
```{r}
my_summary <- function(df, cols) {
df |>
summarise(across({{ cols }},
list(mean = \(x) mean(x, na.rm = TRUE),
sdev = \(x) sd(x, na.rm = TRUE))),
.groups = "drop")
}
```
## Try it out
```{r}
penguins |>
group_by(species) |>
my_summary(ends_with("mm"))
```
## A improved solution
Include a default.
```{r}
my_summary <- function(df, cols = where(is.numeric)) {
df |>
summarise(across({{cols}},
list(mean = \(x) mean(x, na.rm = TRUE),
sdev = \(x) sd(x, na.rm = TRUE))),
.groups = "drop")
}
```
## Try it out
```{r}
penguins |>
select(-year) |>
my_summary()
```
## Summary
- you already knew some iteration: `group_by()`, `facet_wrap()`
- `across()` iterates over columns
- choose columns with familiar `select()` spec
- pass functions without their `()`
- use anonymous functions to add arguments
- use a list to use multiple functions
- specify the names
- You can use `across()` in functions!