-
Notifications
You must be signed in to change notification settings - Fork 1
/
07_dplyr.Rmd
661 lines (515 loc) · 17 KB
/
07_dplyr.Rmd
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
---
title: "07_dplyr"
output: html_document
---
class: middle, center, inverse
layout: false
# 4.5 `dplyr`:<br><br>A Grammar of Data Manipulation
---
background-image: url(https://raw.githubusercontent.com/tidyverse/dplyr/master/man/figures/logo.png)
background-position: 97.5% 2.5%
background-size: 7.5%
layout: true
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
`dplyr` provides a set of functions for manipulating data frame objects (e.g., `tibbles`) while relying on a consistent grammar. Functions are intuitively represented by "verbs" that reflect the underlying operations and always output a new or modified `tibble`.
**Operations on rows:**
- `filter()` picks rows that meet one or several logical criteria
- `slice()` picks rows based on their location in the data
- `arrange()` changes the order of rows
**Operations on columns:**
- `select()` picks respectively drops certain columns
- `rename()` changes the column names
- `relocate()` changes the order of columns
- `mutate()` transforms the column values and/or creates new columns
**Operations on grouped data:**
- `group_by()` partitions data based on one or several columns
- `summarise()` reduces a group of data into a single row
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
```{r, echo=F, out.height='40%', out.width='40%', dpi=100, out.extra='style="float:right; padding:10px"'}
knitr::include_graphics(
"https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/dplyr_filter.jpg"
)
```
**Operations on rows:** `filter()` picks rows that meet one or several logical criteria
Filter for all penguins of `species` "Adelie".
```{r, eval=F}
penguins %>%
filter(species == "Adelie")
```
Filter for all penguins with a missing value in the `bill_length_mm` measurement.
```{r, eval=F}
penguins %>%
filter(is.na(bill_length_mm) == T)
# filter(!is.na(bill_length_mm) == F)
```
Filter for all penguins observed prior to `year` 2008 or subsequent to `year` 2008 and where the body mass (`body_mass_g`) lies between 3,800 and 4,000 grams.
```{r, eval=F}
penguins %>%
filter(between(body_mass_g, 3800, 4000) & (year < 2008 | year > 2008))
```
???
- Note that using `=` instead of `==` is a common mistakes for beginners (`<-` = `=`).
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on rows:** `slice()` picks rows based on their location in the data
.panelset[
.panel[.panel-name[slide()]
Pick rows based on their index.
```{r}
penguins %>%
slice(23:27)
```
]
.panel[.panel-name[slice_head()]
Pick the first `n` rows (vice versa for `slice_tail()`).
```{r}
penguins %>%
slice_head(n = 5) # alternatively: slice_head(frac = 0.05)
```
]
.panel[.panel-name[slice_sample()]
Pick a random sample of `n` rows (with or without replacement).
```{r}
penguins %>%
slice_sample(n = 5)
```
]
.panel[.panel-name[slice_max()]
Pick the `n` rows with the largest value (vice versa for `slice_min()`).
```{r}
penguins %>%
slice_max(bill_length_mm, n = 5)
```
]
]
???
- slice_sample to generate bootstrapped samples
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on rows:** `arrange()` changes the order of rows
.panelset[
.panel[.panel-name[Ascending]
Return the five penguins with the smallest body mass.
```{r}
penguins %>%
arrange(body_mass_g) %>%
slice_head(n = 5) # equivalent to: slice_min(body_mass_g, n = 3)
```
]
.panel[.panel-name[Descending]
Return the five penguins with the highest body mass.
```{r}
penguins %>%
arrange(desc(body_mass_g)) %>%
slice_head(n = 5) # equivalent to: slice_max(body_mass_g, n = 3)
```
]
]
???
- arrange by default always sorts from smallest to largest
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on columns:** `select()` picks respectively drops certain columns
.panelset[
.panel[.panel-name[select() by index]
```{r}
penguins %>%
select(1:3) %>%
glimpse
```
]
.panel[.panel-name[select() by name]
```{r}
penguins %>%
select(species, island, bill_length_mm) %>%
glimpse
```
]
]
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on columns:** `select()` picks respectively drops certain columns (using `tidyselect` helpers)
.panelset[
.panel[.panel-name[everything()]
Select all columns.
```{r}
penguins %>%
select(everything()) %>%
glimpse
```
]
.panel[.panel-name[last_col()]
Select the last column in the data frame.
```{r}
penguins %>%
select(last_col()) %>%
glimpse
```
]
.panel[.panel-name[starts_with()]
Select columns which names start with a certain string.
```{r}
penguins %>%
select(starts_with("bill")) %>%
glimpse
```
]
.panel[.panel-name[ends_with()]
Select columns which names end with a certain string.
```{r}
penguins %>%
select(ends_with("mm")) %>%
glimpse
```
]
.panel[.panel-name[contains()]
Select columns which name contains a certain string.
```{r}
penguins %>%
select(contains("e") & contains("a")) %>%
glimpse
```
]
.panel[.panel-name[machtes()]
Select columns based on a regular expression ([regex](https://www.rexegg.com/regex-quickstart.html)).
```{r}
penguins %>%
select(matches("_\\w*_mm$")) %>%
glimpse
```
]
.panel[.panel-name[where()]
Select columns for which a function evaluates to `TRUE`.
```{r}
penguins %>%
select(where(is.numeric)) %>%
glimpse
```
]
]
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on columns:** `select()` picks respectively drops certain columns
Which columns are returned by the following queries?
```{r, eval=F}
penguins %>%
select(starts_with("s"))
```
```{r, eval=F}
penguins %>%
select(ends_with("mm"))
```
```{r, eval=F}
penguins %>%
select(contains("mm"))
```
```{r, eval=F}
penguins %>%
select(-contains("mm"))
```
```{r, eval=F}
penguins %>%
select(where(~ is.numeric(.))) %>% # equivalent to: select(where(is.numeric))
select(where(~ mean(., na.rm = T) > 1000))
```
???
deselect:
- if you want to deselect something put a minus in front
where:
- feed a function that takes a vector and returns T or F
- when using a function within another function you usually require the formula (~) notation (see `purrr` part), except when only using a function with one argument
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on columns:** `rename()` changes the column names
Change the name of the column `body_mass_g` (`sex`) to `bm` (`gender`).
```{r}
penguins %>% rename(bm = body_mass_g, gender = sex) %>%
colnames()
```
Convert the name of the columns that include the string `"mm"` to upper case.
```{r}
penguins %>% rename_with(.fn = toupper, .cols = contains("mm")) %>%
colnames()
```
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
```{r, echo=F, out.height='40%', out.width='40%', dpi=100, out.extra='style="float:right; padding:10px"'}
knitr::include_graphics(
"https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/dplyr_relocate.png"
)
```
**Operations on columns:** `relocate()` changes the order of columns
Change the order of columns in the `tibble` according to the following scheme:
1. place `species` after `body_mass_g`
2. place `sex` before `species`
3. place `island` at the end
```{r}
penguins %>%
relocate(species, .after = body_mass_g) %>%
relocate(sex, .before = species) %>%
relocate(island, .after = last_col()) %>%
colnames()
```
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on columns:** `mutate()` transforms the column values and/or creates new columns
Create a new `bm_kg` variable which reflects `body_mass_g` measured in kilo grams.
```{r}
penguins %>%
mutate(bm_kg = body_mass_g / 1000, .keep = "all", .after = island) %>%
slice_head(n = 5)
```
- Use the `.keep` argument to specify which columns to keep after manipulation.
- Use the `.before`/`.after` arguments to specify the position of the new column.
- For overriding a given column simply use the same column name.
- For keeping only the new column use `dplyr::transmute()`.
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
```{r, echo=F, out.height='40%', out.width='40%', dpi=100, out.extra='style="float:right; padding:10px"'}
knitr::include_graphics(
"https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/dplyr_case_when.png"
)
```
**Operations on columns:** `mutate()` transforms the column values and/or creates new columns
Create a *one-hot encoded* variable for `sex`.
```{r}
penguins %>%
mutate(
sex_binary = case_when(
sex == "male" ~ 1,
sex == "female" ~ 0),
.keep = "all", .after = island
) %>%
slice_head(n = 3)
```
.footnote[
_**One-hot Encoding:** Encoding a categorical variable with `C` factor levels into `C` dummies (often in modeling you create `C-1` dummies otherwise you have a perfect linear combination of the variables)._
]
???
case_when:
- vectorized version of if_else
- two-sided formulas: LHS tests the condition, RHS specifies the replacement value
- for unmatched cases, the function returns NA
- use LHS `TRUE` to capture all cases not explicitly specified beforehand
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
```{r, echo=F, out.height='40%', out.width='40%', out.extra='style="float:right; padding:10px"'}
knitr::include_graphics(
"https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/dplyr_across.png"
)
```
**Operations on columns:** `mutate()` transforms the column values and/or creates new columns
Transform measurement variables to meters.
```{r}
penguins %>%
mutate(
across(contains("mm"), ~ . / 1000),
.keep = "all"
) %>%
slice_head(n = 3)
```
???
across:
- apply same transformation across multiple columns
- allows you to use the semantics you know from the `select()` function
- does not require you to explicitly specify a column name as it only transform existing columns
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
```{r, echo=F, out.height='40%', out.width='40%', dpi=100, out.extra='style="float:right; padding:10px"'}
knitr::include_graphics(
"https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/dplyr_across.png"
)
```
**Operations on columns:** `mutate()` transforms the column values and/or creates new columns
Define `species`, `island` and `sex` as a categorical variable, i.e. *factors*, using `across()`.
```{r}
penguins %>%
mutate(
across(where(is.character), as.factor),
.keep = "all"
) %>%
slice_head(n = 3)
```
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on grouped data:** `group_by()` partitions data based on one or several columns
```{r}
penguins %>% group_by(species)
```
Use `group_keys()`, `group_indices()` and `group_vars()` to access grouping keys, group indices per row and grouping variables.
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on grouped data:** `group_by()` partitions data based on one or several columns
Under the hood `group_by()` changes the representation of the `tibble` and transforms it into a grouped data frame (`grouped_df`). This allows us to operate on the subgroups individually using `summarise()`.
--
**Operations on grouped data:** `summarise()` reduces a group of data into a single row
.panelset[
.panel[.panel-name[univariate]
```{r}
penguins %>% group_by(species) %>% summarise(count = n(), .groups = "drop")
```
]
.panel[.panel-name[bivariate]
```{r}
penguins %>% group_by(species, sex) %>% summarise(count = n(), .groups = "drop")
```
]
]
???
- use `.groups = ` to indicate what happens to the groups after summarising them
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on grouped data:** `group_by()` partitions data based on one or several columns and `summarise()` reduces a group of data into a single row
```{r}
penguins %>%
group_by(species) %>%
summarise(
across(contains("mm"), ~ mean(., na.rm = T), .names = "{.col}_avg"),
.groups = "drop"
)
```
Using `group_by()`, followed by `summarise()` and `ungroup()` reflects the **split-apply-combine paradigm** in data analysis: Split the data into partitions, apply some function to the data and then merge the results.
???
- the true potential is unleashed if you combine `group_by` and `summarise`
- split-apply-combine paradigm particularly useful in parallel processing
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Operations on grouped data:** `group_by()` partitions data based on one or several columns and `summarise()` reduces a group of data into a single row
```{r, echo=F, out.height='60%', out.width='60%', out.extra='style="float:left; padding:10px"', dpi=100}
knitr::include_graphics("https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/group_by_ungroup.png")
```
<br>
*Note: Instead of using `ungroup()` you may also set the `.groups` argument in `summarise()` equal to "drop".*
*But never forget to ungroup your data, otherwise you may run into errors later on in your analysis!*
???
- now lets look at some more advanced use cases
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Stacked `group_by()`:** Use `.add = T` to add new grouping variables (otherwise the first is overridden)
```{r}
penguins %>%
group_by(species) %>%
group_by(year, .add = T) # equivalent to: group_by(species, year)
```
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Apply multiple summary functions:** Provide a list of `purrr`-style functions to `across()`
```{r}
penguins %>%
group_by(species) %>%
summarise(
across(
contains("mm"),
list(avg = ~ mean(., na.rm = T), sd = ~ sd(., na.rm = T)),
.names = "{.col}_{.fn}"
),
.groups = "drop"
)
```
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Changed behavior of `mutate()`:** Summary functions, e.g., `mean()` or `sd()` now operate on partitions of the data instead of on the whole data
```{r}
penguins %>%
group_by(species) %>%
mutate(stand_bm = (body_mass_g - mean(body_mass_g, na.rm = T)) / sd(body_mass_g, na.rm = T)) %>%
glimpse
```
???
- here example of the z-transformation on a group level
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**`group_by()` a transformed column:** Provide a `mutate()`-like expression in your `group_by()` statement
```{r}
bm_breaks <- mean(penguins$body_mass_g, na.rm = T) - (-3:3) * sd(penguins$body_mass_g, na.rm = T)
penguins %>%
group_by(species, bm_bin = cut(body_mass_g, breaks = bm_breaks)) %>%
summarise(count = n(), .groups = "drop")
```
???
1. compute bins for body mass, the amount of standard deviations from the mean
2. group by data according to these bins (create bins in `group_by()` command)
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Changed behavior of `filter()`:** Filters now operate on partitions of the data instead of on the whole data
```{r}
penguins %>%
group_by(species, island) %>%
filter(flipper_length_mm == max(flipper_length_mm, na.rm = T))
```
???
- Group by all unique `species`-`island` combinations and filter for the penguins with the maximal flipper length per combination
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Nesting of grouped data:** Usually, you will find it more intuitive to use `group_by()` followed by `nest()` to produce a nested data frame compared to the example in [section 4.4](#tidyr_nest).
```{r}
penguins %>%
group_by(species, year) %>%
tidyr::nest()
```
.footnote[
*Note: Find more information about `group_by()` by running `vignette("grouping")`.*
]
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
**Other selected `dplyr` operations:**
.panelset[
.panel[.panel-name[distinct()]
`distinct()` selects only unique rows.
```{r}
penguins %>%
distinct(species, island)
```
]
.panel[.panel-name[pull()]
`pull()` extracts single columns as vectors.
```{r, echo=F}
options(max.print=100)
```
```{r}
penguins %>%
pull(year) # equivalent to: penguins$year
```
```{r, echo=F}
options(max.print=1000)
```
]
.panel[.panel-name[if_else()]
`if_else()` applies a vectorized if-else-statement.
```{r}
penguins %>% select(species, island, body_mass_g) %>%
mutate(penguin_size = if_else(body_mass_g < 3500, "tiny penguin", "big penguin"))
```
]
.panel[.panel-name[lag()]
`lag()` shifts column values by an offset of `n` forward.
```{r}
penguins %>% select(species, body_mass_g) %>%
mutate(lagged_bm = lag(body_mass_g, n = 1))
```
]
.panel[.panel-name[lead()]
`lead()` shifts column values by an offset of `n` backward.
```{r}
penguins %>% select(species, body_mass_g) %>%
mutate(lead_bm = lead(body_mass_g, n = 2))
```
]
.panel[.panel-name[join()]
`left_join()`, `right_join()`, `inner_join()` and `full_join()` enable to merge different data frames by matching rows based on keys (similar to joins performed in SQL).
]
]
.pull-right[.pull-right[.footnote[
*Note: Find more information about `dplyr` by running `vignette("dplyr")` and consulting the official [cheat sheet](https://raw.githubusercontent.com/rstudio/cheatsheets/master/data-transformation.pdf).*
]]]
---
## 4.5 `dplyr`: A Grammar of Data Manipulation
.center[**Similarities between `dplyr` and `SQL` statements:**]
```{r, echo=F, out.width='70%', out.height='70%', fig.align='center'}
knitr::include_graphics("./img/sql-tidyverse.png")
```
.center[
*Src: [Steves (2021)](https://www.rstudio.com/resources/rstudioglobal-2021/the-dynamic-duo-sql-and-r/)*
]