-
Notifications
You must be signed in to change notification settings - Fork 24
/
01-class_05_transform-data.Rmd
1250 lines (976 loc) Β· 28.5 KB
/
01-class_05_transform-data.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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Transform data with dplyr"
author: "Andrew Heiss"
date: "August 23, 2021"
output:
xaringan::moon_reader:
lib_dir: "libs"
chakra: "libs/remark-latest.min.js"
css: ["default", "css/ath-slides.css", "css/ath-inferno-fonts.css"]
seal: false
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
ratio: "16:9"
navigation:
scroll: false
# beforeInit: "libs/offline-mathjax.js"
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE,
fig.retina = 3, fig.align = "center")
```
```{r packages-data, include=FALSE}
library(tidyverse)
library(gapminder)
library(countdown)
library(flair)
library(knitr)
library(kableExtra)
```
```{r xaringanExtra, echo=FALSE}
xaringanExtra::use_xaringan_extra(c("tile_view"))
```
class: center middle main-title section-title-2
# Transform data<br>with dplyr
.class-info[
<figure>
<img src="img/01-class/04/dplyr.png" alt="dplyr" title="dplyr" width="15%">
</figure>
]
---
layout: false
class: title title-2 section-title-inv-2
# Your turn #0: Load data
1. Run the setup chunk
2. Take a look at the `gapminder` data
```{r echo=FALSE}
countdown(minutes = 2, play_sound = TRUE, font_size = "1.5em")
```
---
.small[
```{r show-gapminder}
gapminder
```
]
---
class: title title-2
# The tidyverse
<figure>
<img src="img/01-class/02/tidyverse-language.png" alt="tidyverse and language" title="tidyverse and language" width="100%">
</figure>
???
From "Master the Tidyverse" by RStudio
---
class: title title-2
# The tidyverse
.center[
<figure>
<img src="img/01-class/02/tidyverse.png" alt="The tidyverse" title="The tidyverse" width="50%">
</figure>
]
---
class: title title-2
# dplyr: verbs for manipulating data
<table>
<tr>
<td>Extract rows with <code>filter()</code></td>
<td><img src="img/01-class/04/filter.png" alt="filter" title="filter" height="80px"></td>
</tr>
<tr>
<td>Extract columns with <code>select()</code></td>
<td><img src="img/01-class/04/select.png" alt="select" title="select" height="80px"></td>
</tr>
<tr>
<td>Arrange/sort rows with <code>arrange()</code></td>
<td><img src="img/01-class/04/arrange.png" alt="arrange" title="arrange" height="80px"></td>
</tr>
<tr>
<td>Make new columns with <code>mutate()</code></td>
<td><img src="img/01-class/04/mutate.png" alt="mutate" title="mutate" height="80px"></td>
</tr>
<tr>
<td>Make group summaries with<br><code>group_by() %>% summarize()</code></td>
<td><img src="img/01-class/04/summarize.png" alt="summarize" title="summarize" height="80px"></td>
</tr>
</table>
---
class: center middle section-title section-title-2
# `filter()`
---
layout: false
class: title title-2
# `filter()`
.box-inv-2[Extract rows that meet some sort of test]
.pull-left[
```{r show-filter-template, echo=FALSE, tidy=FALSE}
decorate('
filter(.data = DATA, ...)
', eval = FALSE) %>%
flair("DATA", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair("...", background = "#FFD0CF", before = "<b>", after = "</b>")
```
]
.pull-right[
- <b><span style="background: #FFDFD1">`DATA`</span></b> = Data frame to transform
- <b><span style="background: #FFD0CF">`...`</span></b> = One or more tests <br>.small[`filter()` returns each row for which the test is TRUE]
]
---
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(.data = gapminder, country == "Denmark")
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('country == "Denmark"', background = "#FFD0CF", before = "<b>", after = "</b>")
```
.pull-left[
```{r echo=FALSE}
gapminder %>%
select(country, continent, year) %>%
head(5) %>%
mutate(year = as.character(year)) %>%
bind_rows(tibble(country = "β¦", continent = "β¦", year = "β¦")) %>%
knitr::kable(format = "html")
```
]
--
.pull-right[
```{r echo=FALSE}
gapminder %>%
select(country, continent, year) %>%
filter(country == "Denmark") %>%
head(6) %>%
knitr::kable(format = "html")
```
]
---
class: title title-2
# `filter()`
.pull-left[
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(.data = gapminder,
country == "Denmark")
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('country == "Denmark"', background = "#FFD0CF", before = "<b>", after = "</b>")
```
]
.pull-right[
.box-inv-2[One `=` sets an argument]
.box-inv-2[Two `==` tests if equal<br>.small[returns TRUE or FALSE)]]
]
---
class: title title-2
# Logical tests
<table>
<tr>
<th class="cell-center">Test</th>
<th class="cell-left">Meaning</th>
<th class="cell-center">Test</th>
<th class="cell-left">Meaning</th>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">x < y</code></td>
<td class="cell-left">Less than</td>
<td class="cell-center"><code class="remark-inline-code">x %in% y</code></td>
<td class="cell-left">In (group membership)</td>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">x > y</code></td>
<td class="cell-left">Greater than</td>
<td class="cell-center"><code class="remark-inline-code">is.na(x)</code></td>
<td class="cell-left">Is missing</td>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">==</code></td>
<td class="cell-left">Equal to</td>
<td class="cell-center"><code class="remark-inline-code">!is.na(x)</code></td>
<td class="cell-left">Is not missing</td>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">x <= y</code></td>
<td class="cell-left">Less than or equal to</td>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">x >= y</code></td>
<td class="cell-left">Greater than or equal to</td>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">x != y</code></td>
<td class="cell-left">Not equal to</td>
</tr>
</table>
---
class: title title-2 section-title-inv-2
# Your turn #1: Filtering
.box-2[Use `filter()` and logical tests to showβ¦]
1. The data for Canada
2. All data for countries in Oceania
3. Rows where the life expectancy is greater than 82
```{r echo=FALSE}
countdown(minutes = 4)
```
---
.medium[
```{r eval=FALSE}
filter(gapminder, country == "Canada")
```
]
--
.medium[
```{r eval=FALSE}
filter(gapminder, continent == "Oceania")
```
]
--
.medium[
```{r eval=FALSE}
filter(gapminder, lifeExp > 82)
```
]
---
class: title title-2
# Common mistakes
.pull-left[
.box-inv-2[Using `=` instead of `==`]
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder,
country = "Canada")
', eval = FALSE) %>%
flair("=", color = "#FF4136", before = "<b>", after = "</b>")
```
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder,
country == "Canada")
', eval = FALSE) %>%
flair("==", color = "#2ECC40", before = "<b>", after = "</b>")
```
]
--
.pull-right[
.box-inv-2[Quote use]
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder,
country == Canada)
', eval = FALSE) %>%
flair("Canada", color = "#FF4136", before = "<b>", after = "</b>")
```
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder,
country == "Canada")
', eval = FALSE) %>%
flair('"Canada"', color = "#2ECC40", before = "<b>", after = "</b>")
```
]
---
class: title title-2
# `filter()` with multiple conditions
.box-inv-2[Extract rows that meet *every* test]
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder, country == "Denmark", year > 2000)
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('country == "Denmark", year > 2000', background = "#FFD0CF", before = "<b>", after = "</b>")
```
---
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder, country == "Denmark", year > 2000)
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('country == "Denmark", year > 2000', background = "#FFD0CF", before = "<b>", after = "</b>")
```
.pull-left[
```{r echo=FALSE}
gapminder %>%
select(country, continent, year) %>%
head(5) %>%
mutate(year = as.character(year)) %>%
bind_rows(tibble(country = "β¦", continent = "β¦", year = "β¦")) %>%
knitr::kable(format = "html")
```
]
--
.pull-right[
```{r echo=FALSE}
gapminder %>%
select(country, continent, year) %>%
filter(country == "Denmark", year > 2000) %>%
head(6) %>%
knitr::kable(format = "html")
```
]
---
class: title title-2
# Boolean operators
<table>
<tr>
<th class="cell-center">Operator</th>
<th class="cell-center">Meaning</th>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">a & b</code></td>
<td class="cell-center">and</td>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">a | b</code></td>
<td class="cell-center">or</td>
</tr>
<tr>
<td class="cell-center"><code class="remark-inline-code">!a</code></td>
<td class="cell-center">not</td>
</tr>
</table>
---
class: title title-2
# Default is "and"
.box-inv-2[These do the same thing:]
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder, country == "Denmark", year > 2000)
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('country == "Denmark", year > 2000', background = "#FFD0CF", before = "<b>", after = "</b>")
```
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder, country == "Denmark" & year > 2000)
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('country == "Denmark" & year > 2000', background = "#FFD0CF", before = "<b>", after = "</b>")
```
---
class: title title-2 section-title-inv-2
# Your turn #2: Filtering
.box-2[Use `filter()` and Boolean logical tests to showβ¦]
1. Canada before 1970
2. Countries where life expectancy in 2007 is below 50
3. Countries where life expectancy in 2007 is below 50 and are not in Africa
```{r echo=FALSE}
countdown(minutes = 4)
```
---
```{r eval=FALSE}
filter(gapminder, country == "Canada", year < 1970)
```
--
```{r eval=FALSE}
filter(gapminder, year == 2007, lifeExp < 50)
```
--
```{r eval=FALSE}
filter(gapminder, year == 2007, lifeExp < 50,
continent != "Africa")
```
---
class: title title-2
# Common mistakes
.pull-left[
.box-inv-2[Collapsing multiple tests<br>into one]
.small-code[
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder, 1960 < year < 1980)
', eval = FALSE) %>%
flair("1960 < year < 1980", color = "#FF4136", before = "<b>", after = "</b>")
```
]
.small-code[
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder,
year > 1960, year < 1980)
', eval = FALSE) %>%
flair("year > 1960, year < 1980", color = "#2ECC40", before = "<b>", after = "</b>")
```
]
]
--
.pull-right[
.box-inv-2[Using multiple tests<br>instead of `%in%`]
.small-code[
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder,
country == "Mexico",
country == "Canada",
country == "United States")
', eval = FALSE) %>%
flair('country == "Mexico",
country == "Canada",
country == "United States"', color = "#FF4136", before = "<b>", after = "</b>")
```
]
.small-code[
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder,
country %in% c("Mexico", "Canada",
"United States"))
', eval = FALSE) %>%
flair('country %in% c("Mexico", "Canada",
"United States")', color = "#2ECC40", before = "<b>", after = "</b>")
```
]
]
---
class: title title-2
# Common syntax
.box-inv-2[Every dplyr verb function follows the same pattern]
.box-inv-2[First argument is a data frame; returns a data frame]
.pull-left[
```{r echo=FALSE, tidy=FALSE}
decorate('
VERB(DATA, ...)
', eval = FALSE) %>%
flair("VERB", background = "#EFB3FF", before = "<b>", after = "</b>") %>%
flair("DATA", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair("...", background = "#FFD0CF", before = "<b>", after = "</b>")
```
]
.pull-right[
- <b><span style="background: #EFB3FF">`VERB`</span></b> = dplyr function/verb
- <b><span style="background: #FFDFD1">`DATA`</span></b> = Data frame to transform
- <b><span style="background: #FFD0CF">`...`</span></b> = Stuff the verb does
]
---
class: title title-2
# `mutate()`
.box-inv-2[Create new columns]
.pull-left[
```{r echo=FALSE, tidy=FALSE}
decorate('
mutate(.data, ...)
', eval = FALSE) %>%
flair(".data", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair("...", background = "#FFD0CF", before = "<b>", after = "</b>")
```
]
.pull-right[
- <b><span style="background: #FFDFD1">`DATA`</span></b> = Data frame to transform
- <b><span style="background: #FFD0CF">`...`</span></b> = Columns to make
]
---
```{r echo=FALSE, tidy=FALSE}
decorate('
mutate(gapminder, gdp = gdpPercap * pop)
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('gdp = gdpPercap * pop', background = "#FFD0CF", before = "<b>", after = "</b>")
```
.pull-left.small[
```{r echo=FALSE}
gapminder %>%
select(country, year, gdpPercap, pop) %>%
head(5) %>%
mutate_at(vars(year, gdpPercap, pop), ~as.character(.)) %>%
bind_rows(tibble(country = "β¦", year = "β¦", gdpPercap = "β¦", pop = "β¦")) %>%
knitr::kable(format = "html")
```
]
--
.pull-right.small[
```{r echo=FALSE}
gapminder %>%
mutate(gdp = gdpPercap * pop) %>%
mutate(`β¦` = "β¦") %>%
select(country, year, `β¦`, gdp) %>%
head(6) %>%
knitr::kable(format = "html")
```
]
---
```{r echo=FALSE, tidy=FALSE}
decorate('
mutate(gapminder, gdp = gdpPercap * pop,
pop_mil = round(pop / 1000000))
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('gdp = gdpPercap * pop,', background = "#FFD0CF", before = "<b>", after = "</b>") %>%
flair('pop_mil = round(pop / 1000000)', background = "#FFD0CF", before = "<b>", after = "</b>")
```
.pull-left.small[
```{r echo=FALSE}
gapminder %>%
select(country, year, gdpPercap, pop) %>%
head(5) %>%
mutate_at(vars(year, gdpPercap, pop), ~as.character(.)) %>%
bind_rows(tibble(country = "β¦", year = "β¦", gdpPercap = "β¦", pop = "β¦")) %>%
knitr::kable(format = "html")
```
]
--
.pull-right.small[
```{r echo=FALSE}
gapminder %>%
mutate(gdp = gdpPercap * pop,
pop_mil = round(pop / 1000000)) %>%
mutate(`β¦` = "β¦") %>%
select(country, year, `β¦`, gdp, pop_mil) %>%
head(6) %>%
knitr::kable(format = "html")
```
]
---
class: title title-2
# `ifelse()`
.box-inv-2[Do conditional tests within `mutate()`]
.pull-left[
```{r echo=FALSE, tidy=FALSE}
decorate('
ifelse(TEST,
VALUE_IF_TRUE,
VALUE_IF_FALSE)
', eval = FALSE) %>%
flair("TEST", background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair("VALUE_IF_TRUE", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair("VALUE_IF_FALSE", background = "#CBB5FF", before = "<b>", after = "</b>")
```
]
.pull-right[
- <b><span style="background: #FFC0DC">`TEST`</span></b> = A logical test
- <b><span style="background: #FFDFD1">`VALUE_IF_TRUE`</span></b> = What happens if test is true
- <b><span style="background: #CBB5FF">`VALUE_IF_FALSE`</span></b> = What happens if test is false
]
---
```{r echo=FALSE, tidy=FALSE}
decorate('
mutate(gapminder,
after_1960 = ifelse(year > 1960, TRUE, FALSE))
', eval = FALSE) %>%
flair("year > 1960", background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair("TRUE", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair("FALSE", background = "#CBB5FF", before = "<b>", after = "</b>")
```
```{r echo=FALSE, tidy=FALSE}
decorate('
mutate(gapminder,
after_1960 = ifelse(year > 1960,
"After 1960",
"Before 1960"))
', eval = FALSE) %>%
flair("year > 1960", background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair('"After 1960"', background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair('"Before 1960"', background = "#CBB5FF", before = "<b>", after = "</b>")
```
---
class: title title-2 section-title-inv-2
# Your turn #3: Mutating
.box-2[Use `mutate()` toβ¦]
1. Add an `africa` column that is TRUE if the country is on the African continent
2. Add a column for logged GDP per capita (hint: use `log()`)
3. Add an `africa_asia` column that says βAfrica or Asiaβ if the country is in Africa or Asia, and βNot Africa or Asiaβ if itβs not
```{r echo=FALSE}
countdown(minutes = 5)
```
---
```{r eval=FALSE}
mutate(gapminder, africa = ifelse(continent == "Africa",
TRUE, FALSE))
```
--
```{r eval=FALSE}
mutate(gapminder, log_gdpPercap = log(gdpPercap))
```
--
```{r eval=FALSE}
mutate(gapminder,
africa_asia =
ifelse(continent %in% c("Africa", "Asia"),
"Africa or Asia",
"Not Africa or Asia"))
```
---
class: title title-2
# What if you have multiple verbs?
.box-inv-2.sp-after[Make a dataset for just 2002 *and* calculate logged GDP per capita]
--
.box-inv-2[Solution 1: Intermediate variables]
```{r echo=FALSE, tidy=FALSE}
decorate('
gapminder_2002 <- filter(gapminder, year == 2002)
gapminder_2002_log <- mutate(gapminder_2002,
log_gdpPercap = log(gdpPercap))
', eval = FALSE) %>%
flair("gapminder_2002", background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair("gapminder_2002_log", background = "#FFDFD1", before = "<b>", after = "</b>")
```
---
class: title title-2
# What if you have multiple verbs?
.box-inv-2.sp-after[Make a dataset for just 2002 *and* calculate logged GDP per capita]
.box-inv-2[Solution 2: Nested functions]
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(mutate(gapminder_2002,
log_gdpPercap = log(gdpPercap)),
year == 2002)
', eval = FALSE) %>%
flair("filter(", background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair("year == 2002)", background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair("mutate(gapminder_2002,", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair("log_gdpPercap = log(gdpPercap))", background = "#FFDFD1", before = "<b>", after = "</b>")
```
---
class: title title-2
# What if you have multiple verbs?
.box-inv-2.sp-after[Make a dataset for just 2002 *and* calculate logged GDP per capita]
.box-inv-2[Solution 3: Pipes!]
.box-inv-2[The `%>%` operator (pipe) takes an object on the left<br>and passes it as the first argument of the function on the right]
```{r echo=FALSE, tidy=FALSE}
decorate('
gapminder %>% filter(_____, country == "Canada")
', eval = FALSE) %>%
flair("gapminder", background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair("_____", background = "#FFC0DC", before = "<b>", after = "</b>")
```
---
class: title title-2
# What if you have multiple verbs?
.box-inv-2[These do the same thing!]
```{r echo=FALSE, tidy=FALSE}
decorate('
filter(gapminder, country == "Canada")
', eval = FALSE) %>%
flair("gapminder", background = "#FFC0DC", before = "<b>", after = "</b>")
```
```{r echo=FALSE, tidy=FALSE}
decorate('
gapminder %>% filter(country == "Canada")
', eval = FALSE) %>%
flair("gapminder", background = "#FFC0DC", before = "<b>", after = "</b>")
```
---
class: title title-2
# What if you have multiple verbs?
.box-inv-2.sp-after[Make a dataset for just 2002 *and* calculate logged GDP per capita]
.box-inv-2[Solution 3: Pipes!]
```{r echo=FALSE, tidy=FALSE}
decorate('
gapminder %>%
filter(year == 2002) %>%
mutate(log_gdpPercap = log(gdpPercap))
', eval = FALSE)
```
---
class: title title-2
# `%>%`
```{r echo=FALSE, tidy=FALSE}
decorate('
leave_house(get_dressed(get_out_of_bed(wake_up(me, time = "8:00"), side = "correct"), pants = TRUE, shirt = TRUE), car = TRUE, bike = FALSE)
', eval = FALSE) %>%
flair_args(color = "#0D0887") %>%
flair_funs(background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair_input_vals(color = "#E16462")
```
--
```{r echo=FALSE, tidy=FALSE}
decorate('
me %>%
wake_up(time = "8:00") %>%
get_out_of_bed(side = "correct") %>%
get_dressed(pants = TRUE, shirt = TRUE) %>%
leave_house(car = TRUE, bike = FALSE)
', eval = FALSE) %>%
flair_args(color = "#0D0887") %>%
flair_funs(background = "#FFC0DC", before = "<b>", after = "</b>") %>%
flair_input_vals(color = "#E16462")
```
---
class: title title-2
# `summarize()`
.box-inv-2[Compute a table of summaries]
```{r echo=FALSE, tidy=FALSE}
decorate('
gapminder %>% summarize(mean_life = mean(lifeExp))
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair("mean_life = mean(lifeExp)", background = "#FFD0CF", before = "<b>", after = "</b>")
```
.pull-left.small[
```{r echo=FALSE}
gapminder %>%
select(country, continent, year, lifeExp) %>%
head(4) %>%
mutate_at(vars(year, lifeExp), ~as.character(.)) %>%
bind_rows(tibble(country = "β¦", continent = "β¦", year = "β¦", lifeExp = "β¦")) %>%
knitr::kable(format = "html")
```
]
--
.pull-right.small[
```{r echo=FALSE}
gapminder %>%
summarize(mean_life = mean(lifeExp)) %>%
knitr::kable(format = "html")
```
]
---
class: title title-2
# `summarize()`
```{r echo=FALSE, tidy=FALSE}
decorate('
gapminder %>% summarize(mean_life = mean(lifeExp),
min_life = min(lifeExp))
', eval = FALSE) %>%
flair("gapminder", background = "#FFDFD1", before = "<b>", after = "</b>") %>%
flair("mean_life = mean(lifeExp),", background = "#FFD0CF", before = "<b>", after = "</b>") %>%
flair("min_life = min(lifeExp)", background = "#FFD0CF", before = "<b>", after = "</b>")
```
.pull-left.small[
```{r echo=FALSE}
gapminder %>%
select(country, continent, year, lifeExp) %>%
head(5) %>%
mutate_at(vars(year, lifeExp), ~as.character(.)) %>%
bind_rows(tibble(country = "β¦", continent = "β¦", year = "β¦", lifeExp = "β¦")) %>%
knitr::kable(format = "html")
```
]
--
.pull-right.small[
```{r echo=FALSE}
gapminder %>%
summarize(mean_life = mean(lifeExp),
min_life = min(lifeExp)) %>%
knitr::kable(format = "html")
```
]
---
class: title title-2 section-title-inv-2
# Your turn #4: Summarizing
.box-2[Use `summarize()` to calculateβ¦]
1. The first (minimum) year in the dataset
2. The last (maximum) year in the dataset
3. The number of rows in the dataset (use the cheatsheet)
4. The number of distinct countries in the dataset (use the cheatsheet)
```{r echo=FALSE}
countdown(minutes = 4)
```
---
```{r eval=FALSE}
gapminder %>%
summarize(first = min(year),
last = max(year),
num_rows = n(),
num_unique = n_distinct(country))
```
.small[
```{r echo=FALSE}
gapminder %>%
summarize(first = min(year),
last = max(year),
num_rows = n(),
num_unique = n_distinct(country)) %>%
knitr::kable(format = "html")
```
]
---