-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-data-cleaning-and-manipulation.html
2976 lines (2949 loc) · 176 KB
/
03-data-cleaning-and-manipulation.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.450">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Stat 331/531 Statistical Computing with R - 3 Data Cleaning and Manipulation</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
/* CSS for citations */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging-indent div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}</style>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./04-data-joins-and-transformations.html" rel="next">
<link href="./02-tidy-data-and-basics-of-graphics.html" rel="prev">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-text-highlighting-styles">
<link href="site_libs/quarto-html/quarto-syntax-highlighting-dark.css" rel="prefetch" class="quarto-color-scheme quarto-color-alternate" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" class="quarto-color-scheme" id="quarto-bootstrap" data-mode="light">
<link href="site_libs/bootstrap/bootstrap-dark.min.css" rel="prefetch" class="quarto-color-scheme quarto-color-alternate" id="quarto-bootstrap" data-mode="dark">
<link href="site_libs/quarto-contrib/line-highlight-1.0.0/line-highlight.css" rel="stylesheet"><script id="quarto-search-options" type="application/json">{
"location": "sidebar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "start",
"type": "textbox",
"limit": 20,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script><script src="site_libs/kePrint-0.0.1/kePrint.js"></script><link href="site_libs/lightable-0.0.1/lightable.css" rel="stylesheet">
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script><link rel="stylesheet" href="style.css">
</head>
<body class="nav-sidebar floating slimcontent">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top"><nav class="quarto-secondary-nav"><div class="container-fluid d-flex">
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<i class="bi bi-layout-text-sidebar-reverse"></i>
</button>
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./03-data-cleaning-and-manipulation.html"><span class="chapter-number">3</span> <span class="chapter-title">Data Cleaning and Manipulation</span></a></li></ol></nav>
<a class="flex-grow-1" role="button" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
</a>
<button type="button" class="btn quarto-search-button" aria-label="" onclick="window.quartoOpenSearch();">
<i class="bi bi-search"></i>
</button>
</div>
</nav></header><!-- content --><div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal sidebar-navigation floating overflow-auto"><div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">Stat 331/531 Statistical Computing with R</a>
<div class="sidebar-tools-main tools-wide">
<a href="https://github.com/earobinson95/stat331-calpoly-text" rel="" title="Source Code" class="quarto-navigation-tool px-1" aria-label="Source Code"><i class="bi bi-github"></i></a>
<div class="dropdown">
<a href="" title="Share" id="quarto-navigation-tool-dropdown-0" class="quarto-navigation-tool dropdown-toggle px-1" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Share"><i class="bi bi-share"></i></a>
<ul class="dropdown-menu" aria-labelledby="quarto-navigation-tool-dropdown-0">
<li>
<a class="dropdown-item sidebar-tools-main-item" href="https://twitter.com/intent/tweet?url=%7Curl%7C">
<i class="bi bi-bi-twitter pe-1"></i>
Twitter
</a>
</li>
<li>
<a class="dropdown-item sidebar-tools-main-item" href="https://www.facebook.com/sharer/sharer.php?u=%7Curl%7C">
<i class="bi bi-bi-facebook pe-1"></i>
Facebook
</a>
</li>
<li>
<a class="dropdown-item sidebar-tools-main-item" href="https://www.linkedin.com/sharing/share-offsite/?url=%7Curl%7C">
<i class="bi bi-bi-linkedin pe-1"></i>
LinkedIn
</a>
</li>
</ul>
</div>
<a href="" class="quarto-color-scheme-toggle quarto-navigation-tool px-1" onclick="window.quartoToggleColorScheme(); return false;" title="Toggle dark mode"><i class="bi"></i></a>
</div>
</div>
</div>
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./index.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Preface</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./00-prereading.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Pre-reading</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./01-introduction.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">1</span> <span class="chapter-title">Introduction</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./02-tidy-data-and-basics-of-graphics.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">Tidy Data & Basics of Graphics</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./03-data-cleaning-and-manipulation.html" class="sidebar-item-text sidebar-link active">
<span class="menu-text"><span class="chapter-number">3</span> <span class="chapter-title">Data Cleaning and Manipulation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./04-data-joins-and-transformations.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">4</span> <span class="chapter-title">Data Joins and Transformations</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./05-special-data-types.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">5</span> <span class="chapter-title">Special Data Types</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./06-version-control.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">6</span> <span class="chapter-title">Version Control</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./07-functions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">7</span> <span class="chapter-title">Writing Functions</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./08-functional-programming.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">8</span> <span class="chapter-title">Functional Programming</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./09-statistical-modeling-and-simulation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">9</span> <span class="chapter-title">Regression & Simulating Distributions</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./10-predictive-checks.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">10</span> <span class="chapter-title">Predictive Checks</span></span></a>
</div>
</li>
</ul>
</div>
</nav><div id="quarto-sidebar-glass" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass"></div>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active"><h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#ch3-objectives" id="toc-ch3-objectives" class="nav-link active" data-scroll-target="#ch3-objectives">Objectives</a></li>
<li><a href="#ch3-checkins" id="toc-ch3-checkins" class="nav-link" data-scroll-target="#ch3-checkins">Check-ins</a></li>
<li>
<a href="#tibbles" id="toc-tibbles" class="nav-link" data-scroll-target="#tibbles"><span class="header-section-number">3.1</span> Tibbles</a>
<ul class="collapse">
<li><a href="#learn-more-about-tibbles" id="toc-learn-more-about-tibbles" class="nav-link" data-scroll-target="#learn-more-about-tibbles">Learn more about tibbles</a></li>
</ul>
</li>
<li>
<a href="#introduction-to-dplyr" id="toc-introduction-to-dplyr" class="nav-link" data-scroll-target="#introduction-to-dplyr"><span class="header-section-number">3.2</span> Introduction to <code>dplyr</code></a>
<ul class="collapse">
<li><a href="#main-dplyr-verbs" id="toc-main-dplyr-verbs" class="nav-link" data-scroll-target="#main-dplyr-verbs">Main <code>dplyr</code> verbs</a></li>
</ul>
</li>
<li><a href="#motivation-example-dataset" id="toc-motivation-example-dataset" class="nav-link" data-scroll-target="#motivation-example-dataset">Motivation & Example Dataset</a></li>
<li>
<a href="#filter-pick-cases-rows-based-on-their-values" id="toc-filter-pick-cases-rows-based-on-their-values" class="nav-link" data-scroll-target="#filter-pick-cases-rows-based-on-their-values"><span class="header-section-number">3.3</span> <code>filter()</code>: Pick cases (rows) based on their values</a>
<ul class="collapse">
<li><a href="#common-row-selection-tasks" id="toc-common-row-selection-tasks" class="nav-link" data-scroll-target="#common-row-selection-tasks"><span class="header-section-number">3.3.1</span> Common Row Selection Tasks</a></li>
</ul>
</li>
<li><a href="#select-pick-columns" id="toc-select-pick-columns" class="nav-link" data-scroll-target="#select-pick-columns"><span class="header-section-number">3.4</span> <code>select()</code>: Pick columns</a></li>
<li><a href="#mutate-add-and-transform-variables" id="toc-mutate-add-and-transform-variables" class="nav-link" data-scroll-target="#mutate-add-and-transform-variables"><span class="header-section-number">3.5</span> <code>mutate()</code>: Add and transform variables</a></li>
<li><a href="#summarize" id="toc-summarize" class="nav-link" data-scroll-target="#summarize"><span class="header-section-number">3.6</span> <code>summarize()</code></a></li>
<li><a href="#group_by-group-by-power" id="toc-group_by-group-by-power" class="nav-link" data-scroll-target="#group_by-group-by-power"><span class="header-section-number">3.7</span> <code>group_by()</code> Group By + (?) = Power!</a></li>
<li><a href="#pipe" id="toc-pipe" class="nav-link" data-scroll-target="#pipe"><span class="header-section-number">3.8</span> Pipe Operator</a></li>
<li><a href="#checkin3-1" id="toc-checkin3-1" class="nav-link" data-scroll-target="#checkin3-1">Check-in 3.1: Data Wrangling</a></li>
<li><a href="#additional-resources" id="toc-additional-resources" class="nav-link" data-scroll-target="#additional-resources">Additional Resources</a></li>
<li><a href="#pa-3-identify-the-mystery-college" id="toc-pa-3-identify-the-mystery-college" class="nav-link" data-scroll-target="#pa-3-identify-the-mystery-college">PA 3: Identify the Mystery College</a></li>
<li><a href="#references" id="toc-references" class="nav-link" data-scroll-target="#references">References</a></li>
</ul><div class="toc-actions"><div><i class="bi bi-github"></i></div><div class="action-links"><p><a href="https://github.com/earobinson95/stat331-calpoly-text/edit/main/03-data-cleaning-and-manipulation.qmd" class="toc-action">Edit this page</a></p></div></div></nav>
</div>
<!-- main -->
<main class="content page-columns page-full" id="quarto-document-content"><header id="title-block-header" class="quarto-title-block default"><div class="quarto-title">
<h1 class="title">
<span class="chapter-number">3</span> <span class="chapter-title">Data Cleaning and Manipulation</span>
</h1>
</div>
<div class="quarto-title-meta">
</div>
</header><p><svg aria-hidden="true" role="img" viewbox="0 0 576 512" style="height:1em;width:1.12em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:currentColor;overflow:visible;position:relative;"><path d="M249.6 471.5c10.8 3.8 22.4-4.1 22.4-15.5V78.6c0-4.2-1.6-8.4-5-11C247.4 52 202.4 32 144 32C93.5 32 46.3 45.3 18.1 56.1C6.8 60.5 0 71.7 0 83.8V454.1c0 11.9 12.8 20.2 24.1 16.5C55.6 460.1 105.5 448 144 448c33.9 0 79 14 105.6 23.5zm76.8 0C353 462 398.1 448 432 448c38.5 0 88.4 12.1 119.9 22.6c11.3 3.8 24.1-4.6 24.1-16.5V83.8c0-12.1-6.8-23.3-18.1-27.6C529.7 45.3 482.5 32 432 32c-58.4 0-103.4 20-123 35.6c-3.3 2.6-5 6.8-5 11V456c0 11.4 11.7 19.3 22.4 15.5z"></path></svg> Reading: 18 minute(s) at 200 WPM.</p>
<p><svg aria-hidden="true" role="img" viewbox="0 0 576 512" style="height:1em;width:1.12em;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:currentColor;overflow:visible;position:relative;"><path d="M0 128C0 92.7 28.7 64 64 64H320c35.3 0 64 28.7 64 64V384c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V128zM559.1 99.8c10.4 5.6 16.9 16.4 16.9 28.2V384c0 11.8-6.5 22.6-16.9 28.2s-23 5-32.9-1.6l-96-64L416 337.1V320 192 174.9l14.2-9.5 96-64c9.8-6.5 22.4-7.2 32.9-1.6z"></path></svg> Videos: 60 minutes</p>
<section id="ch3-objectives" class="level2 unnumbered"><h2 class="unnumbered anchored" data-anchor-id="ch3-objectives">Objectives</h2>
<ul>
<li>Apply data manipulation verbs (filter, select, group by, summarize, mutate) to prepare data for analysis</li>
<li>Identify required sequence of steps for data cleaning</li>
<li>Describe step-by-step data cleaning process in lay terms appropriately and understand the consequences of data cleaning steps</li>
<li>Create summaries of data appropriate for analysis or display using data manipulation techniques</li>
</ul></section><section id="ch3-checkins" class="level2 unnumbered check-in"><h2 class="unnumbered anchored" data-anchor-id="ch3-checkins">Check-ins</h2>
<p>There is one check-in for this week:</p>
<ul>
<li><a href="#checkin3-1">Check-in 3.1: Data Wrangling</a></li>
</ul></section><section id="tibbles" class="level2" data-number="3.1"><h2 data-number="3.1" class="anchored" data-anchor-id="tibbles">
<span class="header-section-number">3.1</span> Tibbles</h2>
<p>We have been talking about our data in terms of <code>data.frame</code> objects in R. This is meant to inform you there is another object type in R called <code>tibble</code>s. Essentially, Tibbles <em>are</em> data frames, but they have certain features that make them easier to work with and provide additional cool features that can be useful (e.g., see <code>nest()</code>).</p>
<div class="cell">
<div class="sourceCode" id="cb1" data-source-line-numbers="nil" data-code-line-numbers="nil"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu">tibble</span><span class="op">(</span></span>
<span> team <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"C"</span>, <span class="st">"D"</span><span class="op">)</span>, </span>
<span> points <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">22</span>, <span class="fl">30</span>, <span class="fl">18</span>, <span class="fl">54</span><span class="op">)</span></span>
<span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre data-code-line-numbers=""><code># A tibble: 4 × 2
team points
<chr> <dbl>
1 A 22
2 B 30
3 C 18
4 D 54</code></pre>
</div>
</div>
<p>You can use <code><a href="https://tibble.tidyverse.org/reference/as_tibble.html">as_tibble()</a></code> to convert <code>data.frame</code> objects in R to a <code>tibble</code> object.</p>
<section id="learn-more-about-tibbles" class="level3 unnumbered learn-more"><h3 class="unnumbered anchored" data-anchor-id="learn-more-about-tibbles">Learn more about tibbles</h3>
<p>You can read more about Tibbles in <a href="https://r4ds.had.co.nz/tibbles.html">R for Data Science: Tibbles</a></p>
</section></section><section id="introduction-to-dplyr" class="level2 page-columns page-full" data-number="3.2"><h2 data-number="3.2" class="anchored" data-anchor-id="introduction-to-dplyr">
<span class="header-section-number">3.2</span> Introduction to <code>dplyr</code>
</h2>
<p>In this section, we’re going start learning how to work with data. Generally speaking, data doesn’t come in a form suitable for data visualization or statistical analysis<a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a> - you have to clean it up, create the variables you care about, get rid of those you don’t care about, and so on.</p>
<p>Some people call the process of cleaning and organizing your data “data wrangling”, which is a fantastic way to think about chasing down all of the issues in the data.</p>
<div class="no-row-height column-margin column-container"><div class="">
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://raw.githubusercontent.com/allisonhorst/stats-illustrations/main/rstats-artwork/data_cowboy.png" class="img-fluid figure-img"></p>
<figcaption class="figure-caption">Data wrangling (by Allison Horst)</figcaption></figure>
</div>
</div></div><p>We will be using the <code>tidyverse</code> for this. It’s a meta-package (a package that just loads other packages) that collects packages designed with the same philosophy<a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a> and interface (basically, the commands will use predictable argument names and structure). You’ve already been introduced to parts of the tidyverse - specifically, <code>readr</code> and <code>ggplot2</code>.</p>
<p><code>dplyr</code> (one of the packages in the tidyverse) creates a “grammar of data manipulation” to make it easier to describe different operations. I find the <code>dplyr</code> grammar to be extremely useful when talking about data operations.</p>
<div class="no-row-height column-margin column-container"><div class="">
<div class="youtube-video-container">
<iframe width="100%" height="auto" src="https://www.youtube.com/embed/jgVi2znoHgg" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="">
</iframe>
</div>
</div></div><p>Each <code>dplyr</code> verb describes a common task when doing both exploratory data analysis and more formal statistical modeling. In all tidyverse functions, <strong>data comes first</strong> – literally, as it’s the first argument to any function. In addition, you don’t use <code>df$variable</code> to access a variable - you refer to the variable by its name alone (“bare” names). This makes the syntax much cleaner and easier to read, which is another principle of the tidy philosophy.</p>
<section id="main-dplyr-verbs" class="level3 unnumbered"><h3 class="unnumbered anchored" data-anchor-id="main-dplyr-verbs">Main <code>dplyr</code> verbs</h3>
<ul>
<li><code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code></li>
<li><code><a href="https://dplyr.tidyverse.org/reference/arrange.html">arrange()</a></code></li>
<li><code><a href="https://dplyr.tidyverse.org/reference/select.html">select()</a></code></li>
<li><code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code></li>
<li><code><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarize()</a></code></li>
<li>Use <code><a href="https://dplyr.tidyverse.org/reference/group_by.html">group_by()</a></code> to perform group wise operations</li>
<li>Use the pipe operator (<code>|></code> or <code>%>%</code>) to chain together data wrangling operations</li>
</ul>
<div class="note">
<p><a href="https://github.com/rstudio/cheatsheets/blob/main/data-transformation.pdf">There is an excellent dplyr cheatsheet available from RStudio</a>. You may want to print it out to have a copy to reference as you work through this chapter.</p>
</div>
</section></section><section id="motivation-example-dataset" class="level2 unnumbered"><h2 class="unnumbered anchored" data-anchor-id="motivation-example-dataset">Motivation & Example Dataset</h2>
<p>Last week we learned all about creating graphics in <code>ggplot2</code>. I am hoping to use data visualization as motivation going forward in this class – <strong>how do we get our data look like what we need in order to create the graph we want?</strong></p>
<div class="ex">
<p>Let’s explore how the <code>dplyr</code> verbs work, using the <code>starwars</code> data set, which contains a comprehensive list of the characters in the Star Wars movies and information about their <code>height</code>, <code>mass</code>, <code>hair_color</code>, <code>skin_color</code>, <code>eye_color</code>, <code>birth_year</code>, <code>sex</code>, <code>gender</code>, <code>homeworld</code>, <code>species</code>, <code>films</code>, <code>vehicles</code>, and <code>starships</code>.</p>
<p>This data set is included in the <code>dplyr</code> package, so we load that package and then use the <code><a href="https://rdrr.io/r/utils/data.html">data()</a></code> function to load data set into memory. The loading isn’t complete until we actually use the data set though… so let’s look at our variables and types and print the first few rows.</p>
<div class="cell">
<div class="sourceCode" id="cb3" data-source-line-numbers="nil" data-code-line-numbers="nil"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">starwars</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/str.html">str</a></span><span class="op">(</span><span class="va">starwars</span><span class="op">)</span></span>
<span><span class="va">starwars</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="cell-output-display">
<table data-quarto-postprocess="true" class="table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">name</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">height</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">mass</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">hair_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">skin_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">eye_color</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">birth_year</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">sex</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">gender</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">homeworld</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">species</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Luke Skywalker</td>
<td style="text-align: right;">172</td>
<td style="text-align: right;">77</td>
<td style="text-align: left;">blond</td>
<td style="text-align: left;">fair</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">19.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">C-3PO</td>
<td style="text-align: right;">167</td>
<td style="text-align: right;">75</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">gold</td>
<td style="text-align: left;">yellow</td>
<td style="text-align: right;">112.0</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Droid</td>
</tr>
<tr class="odd">
<td style="text-align: left;">R2-D2</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">32</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">white, blue</td>
<td style="text-align: left;">red</td>
<td style="text-align: right;">33.0</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Naboo</td>
<td style="text-align: left;">Droid</td>
</tr>
<tr class="even">
<td style="text-align: left;">Darth Vader</td>
<td style="text-align: right;">202</td>
<td style="text-align: right;">136</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">white</td>
<td style="text-align: left;">yellow</td>
<td style="text-align: right;">41.9</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Leia Organa</td>
<td style="text-align: right;">150</td>
<td style="text-align: right;">49</td>
<td style="text-align: left;">brown</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">brown</td>
<td style="text-align: right;">19.0</td>
<td style="text-align: left;">female</td>
<td style="text-align: left;">feminine</td>
<td style="text-align: left;">Alderaan</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">Owen Lars</td>
<td style="text-align: right;">178</td>
<td style="text-align: right;">120</td>
<td style="text-align: left;">brown, grey</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">52.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Beru Whitesun lars</td>
<td style="text-align: right;">165</td>
<td style="text-align: right;">75</td>
<td style="text-align: left;">brown</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">47.0</td>
<td style="text-align: left;">female</td>
<td style="text-align: left;">feminine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">R5-D4</td>
<td style="text-align: right;">97</td>
<td style="text-align: right;">32</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">white, red</td>
<td style="text-align: left;">red</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Droid</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Biggs Darklighter</td>
<td style="text-align: right;">183</td>
<td style="text-align: right;">84</td>
<td style="text-align: left;">black</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">brown</td>
<td style="text-align: right;">24.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">Obi-Wan Kenobi</td>
<td style="text-align: right;">182</td>
<td style="text-align: right;">77</td>
<td style="text-align: left;">auburn, white</td>
<td style="text-align: left;">fair</td>
<td style="text-align: left;">blue-gray</td>
<td style="text-align: right;">57.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Stewjon</td>
<td style="text-align: left;">Human</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>We could create a scatterplot of the character’s <code>height</code> by <code>mass</code>, color by <code>species</code>, and facet by <code>homeworld</code>.</p>
<div class="cell">
<div class="sourceCode" id="cb4" data-source-line-numbers="nil" data-code-line-numbers="nil"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op">)</span></span>
<span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">starwars</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">height</span>, </span>
<span> y <span class="op">=</span> <span class="va">mass</span>, </span>
<span> color <span class="op">=</span> <span class="va">species</span><span class="op">)</span></span>
<span> <span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">facet_wrap</a></span><span class="op">(</span><span class="op">~</span> <span class="va">homeworld</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="03-data-cleaning-and-manipulation_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>There is way too much going on in these plots to see anything of importance. Let’s break it down into the parts we are interested in.</p>
</div>
</section><section id="filter-pick-cases-rows-based-on-their-values" class="level2 page-columns page-full" data-number="3.3"><h2 data-number="3.3" class="anchored" data-anchor-id="filter-pick-cases-rows-based-on-their-values">
<span class="header-section-number">3.3</span> <code>filter()</code>: Pick cases (rows) based on their values</h2>
<p>Filter allows us to work with a subset of a larger data frame, keeping only the rows we’re interested in. We provide one or more logical conditions, and only those rows which meet the logical conditions are returned from <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code>. Note that unless we store the result from <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code> in the original object, we don’t change the original.</p>
<div class="no-row-height column-margin column-container"><div class="">
<div class="youtube-video-container">
<iframe width="560" height="315" src="https://www.youtube.com/embed/pPI-prhLYqI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="">
</iframe>
</div>
</div></div><div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://github.com/allisonhorst/stats-illustrations/raw/main/rstats-artwork/dplyr_filter.jpg" class="img-fluid figure-img" alt="Cartoon showing three fuzzy monsters either selecting or crossing out rows of a data table. If the type of animal in the table is “otter” and the site is “bay”, a monster is drawing a purple rectangle around the row. If those conditions are not met, another monster is putting a line through the column indicating it will be excluded. Stylized text reads “dplyr::filter() - keep rows that satisfy your conditions.”"></p>
<figcaption class="figure-caption">dplyr filter() by Allison Horst</figcaption></figure>
</div>
<p>Once the data is set up, filtering the data (selecting certain <strong>rows</strong>) is actually very simple. Of course, we’ve talked about how to use logical indexing before in <a href="00-prereading.html#indexing-matrices">Indexing Matrices</a>, but here we’ll focus on using specific functions to perform the same operation.</p>
<p>The dplyr verb for selecting rows is <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code>. <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code> takes a set of one or more logical conditions, using bare column names and logical operators. Each provided condition is combined using AND.</p>
<div class="ex">
<p>Let’s say we were interested in only the people, we could create a new data set <code>starwars_people</code> and filter on the species variable.</p>
<div class="cell">
<div class="sourceCode" id="cb5" data-source-line-numbers="nil" data-code-line-numbers="nil"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co"># Get only the people</span></span>
<span><span class="va">starwars_people</span> <span class="op"><-</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op">(</span><span class="va">starwars</span>, <span class="va">species</span> <span class="op">==</span> <span class="st">"Human"</span><span class="op">)</span></span>
<span><span class="va">starwars_people</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small" data-quarto-postprocess="true">
<thead><tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">name</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">height</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">mass</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">hair_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">skin_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">eye_color</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">birth_year</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">sex</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">gender</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">homeworld</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">species</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Luke Skywalker</td>
<td style="text-align: right;">172</td>
<td style="text-align: right;">77</td>
<td style="text-align: left;">blond</td>
<td style="text-align: left;">fair</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">19.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">Darth Vader</td>
<td style="text-align: right;">202</td>
<td style="text-align: right;">136</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">white</td>
<td style="text-align: left;">yellow</td>
<td style="text-align: right;">41.9</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Leia Organa</td>
<td style="text-align: right;">150</td>
<td style="text-align: right;">49</td>
<td style="text-align: left;">brown</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">brown</td>
<td style="text-align: right;">19.0</td>
<td style="text-align: left;">female</td>
<td style="text-align: left;">feminine</td>
<td style="text-align: left;">Alderaan</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">Owen Lars</td>
<td style="text-align: right;">178</td>
<td style="text-align: right;">120</td>
<td style="text-align: left;">brown, grey</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">52.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Beru Whitesun lars</td>
<td style="text-align: right;">165</td>
<td style="text-align: right;">75</td>
<td style="text-align: left;">brown</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">47.0</td>
<td style="text-align: left;">female</td>
<td style="text-align: left;">feminine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>We can create the same plot with our new subset of data.</p>
<div class="cell">
<details><summary>Code</summary><div class="sourceCode" id="cb6" data-source-line-numbers="nil" data-code-line-numbers="nil"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">starwars_people</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">height</span>, </span>
<span> y <span class="op">=</span> <span class="va">mass</span>, </span>
<span> color <span class="op">=</span> <span class="va">species</span><span class="op">)</span></span>
<span> <span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">facet_wrap</a></span><span class="op">(</span><span class="op">~</span> <span class="va">homeworld</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details><div class="cell-output-display">
<p><img src="03-data-cleaning-and-manipulation_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>This looks better, but what if we only care about the people who come from Tatooine? Starting with our original starwars data set, we can combine logical AND statements with a comma to define a data subset called <code>starwars_tatoonie_people</code>.</p>
<div class="cell">
<div class="sourceCode" id="cb7" data-source-line-numbers="nil" data-code-line-numbers="nil"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co"># Get only the people who come from Tatooine</span></span>
<span><span class="va">starwars_tatooine_people</span> <span class="op"><-</span> <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op">(</span><span class="va">starwars</span>, <span class="va">species</span> <span class="op">==</span> <span class="st">"Human"</span>, <span class="va">homeworld</span> <span class="op">==</span> <span class="st">"Tatooine"</span><span class="op">)</span></span>
<span><span class="va">starwars_tatooine_people</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small" data-quarto-postprocess="true">
<thead><tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">name</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">height</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">mass</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">hair_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">skin_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">eye_color</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">birth_year</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">sex</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">gender</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">homeworld</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">species</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Luke Skywalker</td>
<td style="text-align: right;">172</td>
<td style="text-align: right;">77</td>
<td style="text-align: left;">blond</td>
<td style="text-align: left;">fair</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">19.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">Darth Vader</td>
<td style="text-align: right;">202</td>
<td style="text-align: right;">136</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">white</td>
<td style="text-align: left;">yellow</td>
<td style="text-align: right;">41.9</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Owen Lars</td>
<td style="text-align: right;">178</td>
<td style="text-align: right;">120</td>
<td style="text-align: left;">brown, grey</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">52.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">Beru Whitesun lars</td>
<td style="text-align: right;">165</td>
<td style="text-align: right;">75</td>
<td style="text-align: left;">brown</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">47.0</td>
<td style="text-align: left;">female</td>
<td style="text-align: left;">feminine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Biggs Darklighter</td>
<td style="text-align: right;">183</td>
<td style="text-align: right;">84</td>
<td style="text-align: left;">black</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">brown</td>
<td style="text-align: right;">24.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<details><summary>Code</summary><div class="sourceCode" id="cb8" data-source-line-numbers="nil" data-code-line-numbers="nil"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">starwars_tatooine_people</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">height</span>, </span>
<span> y <span class="op">=</span> <span class="va">mass</span>, </span>
<span> color <span class="op">=</span> <span class="va">species</span><span class="op">)</span></span>
<span> <span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span></span>
<span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html">facet_wrap</a></span><span class="op">(</span><span class="op">~</span> <span class="va">homeworld</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details><div class="cell-output-display">
<p><img src="03-data-cleaning-and-manipulation_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</div>
<p><br></p>
<section id="useful-comparison-operations-in-r" class="level4 unnumbered note"><h4 class="unnumbered anchored" data-anchor-id="useful-comparison-operations-in-r">Useful comparison operations in R</h4>
<p>We might not always want to only filter on a variable set equal to a certain category or value, the following operations can help you combine logical operations in <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code>.</p>
<ul>
<li>
<code>></code> greater than</li>
<li>
<code><</code> less than</li>
<li>
<code>==</code> equal to</li>
<li>
<code>%in%</code> identifies if an element belongs to a vector</li>
<li>
<code>|</code> or</li>
</ul></section><section id="common-row-selection-tasks" class="level3 page-columns page-full" data-number="3.3.1"><h3 data-number="3.3.1" class="anchored" data-anchor-id="common-row-selection-tasks">
<span class="header-section-number">3.3.1</span> Common Row Selection Tasks</h3>
<p>In <code>dplyr</code>, there are a few helper functions which may be useful when constructing filter statements.</p>
<div class="no-row-height column-margin column-container"><div class="">
<div class="youtube-video-container">
<iframe width="560" height="315" src="https://www.youtube.com/embed/4Qhdc81mpvo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="">
</iframe>
</div>
</div></div><section id="filtering-by-row-number" class="level4 unnumbered"><h4 class="unnumbered anchored" data-anchor-id="filtering-by-row-number">Filtering by row number</h4>
<p><code><a href="https://dplyr.tidyverse.org/reference/row_number.html">row_number()</a></code> is a helper function that is only used inside of another dplyr function (e.g. filter). You might want to keep only even rows, or only the first 10 rows in a table.</p>
<div class="ex">
<p>Notice how we now have C-3PO, Darth Vader, Beru Whites, Anakin Skywalker, etc. (rows 2, 4, 6, …) from the original <code>starwars</code> data set output above.</p>
<div class="cell">
<div class="sourceCode" id="cb9" data-source-line-numbers="nil" data-code-line-numbers="nil"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op">(</span><span class="va">starwars</span>, <span class="op">(</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/row_number.html">row_number</a></span><span class="op">(</span><span class="op">)</span> <span class="op"><a href="https://rdrr.io/r/base/Arithmetic.html">%%</a></span> <span class="fl">2</span> <span class="op">==</span> <span class="fl">0</span><span class="op">)</span><span class="op">)</span> </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item" role="presentation"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" role="tab" aria-controls="tabset-1-1" aria-selected="true">Even Rows</a></li>
<li class="nav-item" role="presentation"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" role="tab" aria-controls="tabset-1-2" aria-selected="false">Original <code>starwars</code></a></li>
</ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" role="tabpanel" aria-labelledby="tabset-1-1-tab">
<div class="cell">
<div class="cell-output-display">
<table data-quarto-postprocess="true" class="table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">name</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">height</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">mass</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">hair_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">skin_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">eye_color</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">birth_year</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">sex</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">gender</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">homeworld</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">species</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">C-3PO</td>
<td style="text-align: right;">167</td>
<td style="text-align: right;">75</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">gold</td>
<td style="text-align: left;">yellow</td>
<td style="text-align: right;">112.0</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Droid</td>
</tr>
<tr class="even">
<td style="text-align: left;">Darth Vader</td>
<td style="text-align: right;">202</td>
<td style="text-align: right;">136</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">white</td>
<td style="text-align: left;">yellow</td>
<td style="text-align: right;">41.9</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Owen Lars</td>
<td style="text-align: right;">178</td>
<td style="text-align: right;">120</td>
<td style="text-align: left;">brown, grey</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">52.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left;">R5-D4</td>
<td style="text-align: right;">97</td>
<td style="text-align: right;">32</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">white, red</td>
<td style="text-align: left;">red</td>
<td style="text-align: right;">NA</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Droid</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Obi-Wan Kenobi</td>
<td style="text-align: right;">182</td>
<td style="text-align: right;">77</td>
<td style="text-align: left;">auburn, white</td>
<td style="text-align: left;">fair</td>
<td style="text-align: left;">blue-gray</td>
<td style="text-align: right;">57.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Stewjon</td>
<td style="text-align: left;">Human</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" role="tabpanel" aria-labelledby="tabset-1-2-tab">
<div class="cell">
<div class="cell-output-display">
<table class="table table-sm table-striped small" data-quarto-postprocess="true">
<thead><tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">name</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">height</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">mass</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">hair_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">skin_color</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">eye_color</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">birth_year</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">sex</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">gender</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">homeworld</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">species</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Luke Skywalker</td>
<td style="text-align: right;">172</td>
<td style="text-align: right;">77</td>
<td style="text-align: left;">blond</td>
<td style="text-align: left;">fair</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">19.0</td>
<td style="text-align: left;">male</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">C-3PO</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">167</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">75</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">NA</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">gold</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">yellow</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">112.0</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">none</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">masculine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Droid</td>
</tr>
<tr class="odd">
<td style="text-align: left;">R2-D2</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">32</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">white, blue</td>
<td style="text-align: left;">red</td>
<td style="text-align: right;">33.0</td>
<td style="text-align: left;">none</td>
<td style="text-align: left;">masculine</td>
<td style="text-align: left;">Naboo</td>
<td style="text-align: left;">Droid</td>
</tr>
<tr class="even">
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Darth Vader</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">202</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">136</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">none</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">white</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">yellow</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">41.9</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">male</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">masculine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Leia Organa</td>
<td style="text-align: right;">150</td>
<td style="text-align: right;">49</td>
<td style="text-align: left;">brown</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">brown</td>
<td style="text-align: right;">19.0</td>
<td style="text-align: left;">female</td>
<td style="text-align: left;">feminine</td>
<td style="text-align: left;">Alderaan</td>
<td style="text-align: left;">Human</td>
</tr>
<tr class="even">
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Owen Lars</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">178</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">120</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">brown, grey</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">light</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">blue</td>
<td style="text-align: right; font-weight: bold; color: black !important; background-color: yellow !important;">52.0</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">male</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">masculine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Tatooine</td>
<td style="text-align: left; font-weight: bold; color: black !important; background-color: yellow !important;">Human</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Beru Whitesun lars</td>
<td style="text-align: right;">165</td>
<td style="text-align: right;">75</td>
<td style="text-align: left;">brown</td>
<td style="text-align: left;">light</td>
<td style="text-align: left;">blue</td>
<td style="text-align: right;">47.0</td>
<td style="text-align: left;">female</td>
<td style="text-align: left;">feminine</td>
<td style="text-align: left;">Tatooine</td>
<td style="text-align: left;">Human</td>
</tr>