-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleaning-data-in-r.html
1168 lines (1127 loc) · 117 KB
/
cleaning-data-in-r.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 lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>9 Cleaning Data in R | R Programming Guidebook Project</title>
<meta name="description" content="9 Cleaning Data in R | R Programming Guidebook Project" />
<meta name="generator" content="bookdown 0.26 and GitBook 2.6.7" />
<meta property="og:title" content="9 Cleaning Data in R | R Programming Guidebook Project" />
<meta property="og:type" content="book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="9 Cleaning Data in R | R Programming Guidebook Project" />
<meta name="author" content="Alec Nguyen" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="joining-data-with-dplyr.html"/>
<link rel="next" href="introduction-to-sql.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
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; }
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;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>About</a></li>
<li class="part"><span><b>I DataCamp</b></span></li>
<li class="chapter" data-level="1" data-path="introduction-to-r.html"><a href="introduction-to-r.html"><i class="fa fa-check"></i><b>1</b> Introduction to R</a>
<ul>
<li class="chapter" data-level="1.1" data-path="introduction-to-r.html"><a href="introduction-to-r.html#intro-to-basics"><i class="fa fa-check"></i><b>1.1</b> Intro to basics</a></li>
<li class="chapter" data-level="1.2" data-path="introduction-to-r.html"><a href="introduction-to-r.html#vectors"><i class="fa fa-check"></i><b>1.2</b> Vectors</a></li>
<li class="chapter" data-level="1.3" data-path="introduction-to-r.html"><a href="introduction-to-r.html#matrices"><i class="fa fa-check"></i><b>1.3</b> Matrices</a></li>
<li class="chapter" data-level="1.4" data-path="introduction-to-r.html"><a href="introduction-to-r.html#factors"><i class="fa fa-check"></i><b>1.4</b> Factors</a></li>
<li class="chapter" data-level="1.5" data-path="introduction-to-r.html"><a href="introduction-to-r.html#data-frames"><i class="fa fa-check"></i><b>1.5</b> Data frames</a></li>
<li class="chapter" data-level="1.6" data-path="introduction-to-r.html"><a href="introduction-to-r.html#lists"><i class="fa fa-check"></i><b>1.6</b> Lists</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="intermediate-r.html"><a href="intermediate-r.html"><i class="fa fa-check"></i><b>2</b> Intermediate R</a>
<ul>
<li class="chapter" data-level="2.1" data-path="intermediate-r.html"><a href="intermediate-r.html#conditionals-and-control-flow"><i class="fa fa-check"></i><b>2.1</b> Conditionals And Control Flow</a></li>
<li class="chapter" data-level="2.2" data-path="intermediate-r.html"><a href="intermediate-r.html#loops"><i class="fa fa-check"></i><b>2.2</b> Loops</a></li>
<li class="chapter" data-level="2.3" data-path="intermediate-r.html"><a href="intermediate-r.html#functions"><i class="fa fa-check"></i><b>2.3</b> Functions</a></li>
<li class="chapter" data-level="2.4" data-path="intermediate-r.html"><a href="intermediate-r.html#the-apply-family"><i class="fa fa-check"></i><b>2.4</b> The apply family</a></li>
<li class="chapter" data-level="2.5" data-path="intermediate-r.html"><a href="intermediate-r.html#utilities"><i class="fa fa-check"></i><b>2.5</b> Utilities</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html"><i class="fa fa-check"></i><b>3</b> Intro to the Tidyverse</a>
<ul>
<li class="chapter" data-level="3.1" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html#data-wrangling"><i class="fa fa-check"></i><b>3.1</b> Data wrangling</a></li>
<li class="chapter" data-level="3.2" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html#data-visualization"><i class="fa fa-check"></i><b>3.2</b> Data visualization</a></li>
<li class="chapter" data-level="3.3" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html#grouping-and-summarizing"><i class="fa fa-check"></i><b>3.3</b> Grouping and summarizing</a></li>
<li class="chapter" data-level="3.4" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html#types-of-visualizations"><i class="fa fa-check"></i><b>3.4</b> Types of visualizations</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html"><i class="fa fa-check"></i><b>4</b> Intro to Data Visualization with ggplot2</a>
<ul>
<li class="chapter" data-level="4.1" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html#introduction"><i class="fa fa-check"></i><b>4.1</b> Introduction</a></li>
<li class="chapter" data-level="4.2" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html#aesthetics"><i class="fa fa-check"></i><b>4.2</b> Aesthetics</a></li>
<li class="chapter" data-level="4.3" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html#geometries"><i class="fa fa-check"></i><b>4.3</b> Geometries</a></li>
<li class="chapter" data-level="4.4" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html#themes"><i class="fa fa-check"></i><b>4.4</b> Themes</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html"><i class="fa fa-check"></i><b>5</b> Working with Data in the Tidyverse</a>
<ul>
<li class="chapter" data-level="5.1" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html#explore-your-data"><i class="fa fa-check"></i><b>5.1</b> Explore your data</a></li>
<li class="chapter" data-level="5.2" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html#tame-your-data"><i class="fa fa-check"></i><b>5.2</b> Tame your data</a></li>
<li class="chapter" data-level="5.3" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html#tidy-your-data"><i class="fa fa-check"></i><b>5.3</b> Tidy your data</a></li>
<li class="chapter" data-level="5.4" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html#transform-your-data"><i class="fa fa-check"></i><b>5.4</b> Transform your data</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html"><i class="fa fa-check"></i><b>6</b> Categorical Data in the Tidyverse</a>
<ul>
<li class="chapter" data-level="6.1" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html#introduction-to-factor-variables"><i class="fa fa-check"></i><b>6.1</b> Introduction to Factor Variables</a></li>
<li class="chapter" data-level="6.2" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html#manipulating-factor-variables"><i class="fa fa-check"></i><b>6.2</b> Manipulating Factor Variables</a></li>
<li class="chapter" data-level="6.3" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html#creating-factor-variables"><i class="fa fa-check"></i><b>6.3</b> Creating Factor Variables</a></li>
<li class="chapter" data-level="6.4" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html#case-study-on-flight-etiquette"><i class="fa fa-check"></i><b>6.4</b> Case Study on Flight Etiquette</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html"><i class="fa fa-check"></i><b>7</b> Data Manipulation with dplyr</a>
<ul>
<li class="chapter" data-level="7.1" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html#transforming-data-with-dplyr"><i class="fa fa-check"></i><b>7.1</b> Transforming Data with dplyr</a></li>
<li class="chapter" data-level="7.2" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html#aggregating-data"><i class="fa fa-check"></i><b>7.2</b> Aggregating Data</a></li>
<li class="chapter" data-level="7.3" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html#selecting-and-transforming-data"><i class="fa fa-check"></i><b>7.3</b> Selecting and Transforming Data</a></li>
<li class="chapter" data-level="7.4" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html#case-study-the-babynames-dataset"><i class="fa fa-check"></i><b>7.4</b> Case Study: The babynames Dataset</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html"><i class="fa fa-check"></i><b>8</b> Joining Data with dplyr</a>
<ul>
<li class="chapter" data-level="8.1" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html#joining-tables"><i class="fa fa-check"></i><b>8.1</b> Joining Tables</a></li>
<li class="chapter" data-level="8.2" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html#left-and-right-joins"><i class="fa fa-check"></i><b>8.2</b> Left and Right Joins</a></li>
<li class="chapter" data-level="8.3" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html#full-semi-and-anti-joins"><i class="fa fa-check"></i><b>8.3</b> Full, Semi, and Anti Joins</a></li>
<li class="chapter" data-level="8.4" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html#case-study-joins-on-stack-overflow-data"><i class="fa fa-check"></i><b>8.4</b> Case Study: Joins on Stack Overflow Data</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html"><i class="fa fa-check"></i><b>9</b> Cleaning Data in R</a>
<ul>
<li class="chapter" data-level="9.1" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html#common-data-problems"><i class="fa fa-check"></i><b>9.1</b> Common Data Problems</a></li>
<li class="chapter" data-level="9.2" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html#categorical-and-text-data"><i class="fa fa-check"></i><b>9.2</b> Categorical and Text Data</a></li>
<li class="chapter" data-level="9.3" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html#advanced-data-problems"><i class="fa fa-check"></i><b>9.3</b> Advanced Data Problems</a></li>
<li class="chapter" data-level="9.4" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html#record-linkage"><i class="fa fa-check"></i><b>9.4</b> Record Linkage</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html"><i class="fa fa-check"></i><b>10</b> Introduction to SQL</a>
<ul>
<li class="chapter" data-level="10.1" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html#selecting-columns"><i class="fa fa-check"></i><b>10.1</b> Selecting columns</a></li>
<li class="chapter" data-level="10.2" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html#filtering-rows"><i class="fa fa-check"></i><b>10.2</b> Filtering rows</a></li>
<li class="chapter" data-level="10.3" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html#aggregate-functions"><i class="fa fa-check"></i><b>10.3</b> Aggregate Functions</a></li>
<li class="chapter" data-level="10.4" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html#sorting-and-grouping"><i class="fa fa-check"></i><b>10.4</b> Sorting and grouping</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html"><i class="fa fa-check"></i><b>11</b> Joining Data in SQL</a>
<ul>
<li class="chapter" data-level="11.1" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html#introduction-to-joins"><i class="fa fa-check"></i><b>11.1</b> Introduction to joins</a></li>
<li class="chapter" data-level="11.2" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html#outer-joins-and-cross-joins"><i class="fa fa-check"></i><b>11.2</b> Outer joins and cross joins</a></li>
<li class="chapter" data-level="11.3" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html#set-theory-clauses"><i class="fa fa-check"></i><b>11.3</b> Set theory clauses</a></li>
<li class="chapter" data-level="11.4" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html#subqueries"><i class="fa fa-check"></i><b>11.4</b> Subqueries</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html"><i class="fa fa-check"></i><b>12</b> Web Scraping in R</a>
<ul>
<li class="chapter" data-level="12.1" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html#introduction-to-html-and-web-scraping"><i class="fa fa-check"></i><b>12.1</b> Introduction to HTML and Web Scraping</a></li>
<li class="chapter" data-level="12.2" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html#navigation-and-selection-with-css"><i class="fa fa-check"></i><b>12.2</b> Navigation and Selection with CSS</a></li>
<li class="chapter" data-level="12.3" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html#advanced-selection-with-xpath"><i class="fa fa-check"></i><b>12.3</b> Advanced Selection with XPATH</a></li>
<li class="chapter" data-level="12.4" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html#scraping-best-practices"><i class="fa fa-check"></i><b>12.4</b> Scraping Best Practices</a></li>
</ul></li>
<li class="part"><span><b>II Econometrics</b></span></li>
<li class="chapter" data-level="13" data-path="ch-2---slr.html"><a href="ch-2---slr.html"><i class="fa fa-check"></i><b>13</b> Ch 2 - SLR</a>
<ul>
<li class="chapter" data-level="13.1" data-path="ch-2---slr.html"><a href="ch-2---slr.html#notes"><i class="fa fa-check"></i><b>13.1</b> Notes</a></li>
<li class="chapter" data-level="13.2" data-path="ch-2---slr.html"><a href="ch-2---slr.html#example-2.3-ceo-salary-and-return-on-equity"><i class="fa fa-check"></i><b>13.2</b> Example 2.3: CEO Salary and Return on Equity</a></li>
<li class="chapter" data-level="13.3" data-path="ch-2---slr.html"><a href="ch-2---slr.html#example-2.4-wage-and-education"><i class="fa fa-check"></i><b>13.3</b> Example 2.4: Wage and Education</a></li>
<li class="chapter" data-level="13.4" data-path="ch-2---slr.html"><a href="ch-2---slr.html#example-2.5-voting-outcomes-and-campaign-expenditures"><i class="fa fa-check"></i><b>13.4</b> Example 2.5: Voting Outcomes and Campaign Expenditures</a></li>
<li class="chapter" data-level="13.5" data-path="ch-2---slr.html"><a href="ch-2---slr.html#example-of-fitted-values-haty"><i class="fa fa-check"></i><b>13.5</b> Example of Fitted Values (<span class="math inline">\(\hat{y}\)</span>)</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html"><i class="fa fa-check"></i><b>14</b> Ch 3 - MLR</a>
<ul>
<li class="chapter" data-level="14.1" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#data"><i class="fa fa-check"></i><b>14.1</b> Data</a>
<ul>
<li class="chapter" data-level="14.1.1" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#summary-statistics"><i class="fa fa-check"></i><b>14.1.1</b> Summary Statistics</a></li>
</ul></li>
<li class="chapter" data-level="14.2" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#regression-model-comparisons"><i class="fa fa-check"></i><b>14.2</b> Regression model comparisons</a></li>
<li class="chapter" data-level="14.3" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#adjusted-r-squared"><i class="fa fa-check"></i><b>14.3</b> Adjusted R-Squared</a></li>
<li class="chapter" data-level="14.4" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#multicollinearity"><i class="fa fa-check"></i><b>14.4</b> Multicollinearity</a>
<ul>
<li class="chapter" data-level="14.4.1" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#variance-inflation-factor-vif"><i class="fa fa-check"></i><b>14.4.1</b> Variance Inflation Factor (VIF)</a></li>
<li class="chapter" data-level="14.4.2" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#joint-hypotheses-test"><i class="fa fa-check"></i><b>14.4.2</b> Joint hypotheses test</a></li>
</ul></li>
<li class="chapter" data-level="14.5" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#testing-linear-combinations-of-parameters"><i class="fa fa-check"></i><b>14.5</b> Testing linear combinations of parameters</a></li>
<li class="chapter" data-level="14.6" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#a-note-on-presentation"><i class="fa fa-check"></i><b>14.6</b> A note on presentation</a></li>
<li class="chapter" data-level="14.7" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#log-transformations"><i class="fa fa-check"></i><b>14.7</b> Log transformations</a>
<ul>
<li class="chapter" data-level="14.7.1" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#histograms"><i class="fa fa-check"></i><b>14.7.1</b> Histograms</a></li>
<li class="chapter" data-level="14.7.2" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#scatter-plots"><i class="fa fa-check"></i><b>14.7.2</b> Scatter plots</a></li>
<li class="chapter" data-level="14.7.3" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#regression-models-with-levels-and-logs"><i class="fa fa-check"></i><b>14.7.3</b> Regression models with levels and logs</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="15" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html"><i class="fa fa-check"></i><b>15</b> Dummy Variables Part 1</a>
<ul>
<li class="chapter" data-level="15.1" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#obtain-and-prepare-data"><i class="fa fa-check"></i><b>15.1</b> Obtain and prepare data</a></li>
<li class="chapter" data-level="15.2" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#define-dummy-variables"><i class="fa fa-check"></i><b>15.2</b> Define dummy variables</a>
<ul>
<li class="chapter" data-level="15.2.1" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#alpha-model"><i class="fa fa-check"></i><b>15.2.1</b> Alpha Model</a></li>
<li class="chapter" data-level="15.2.2" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#beta-model"><i class="fa fa-check"></i><b>15.2.2</b> Beta Model</a></li>
</ul></li>
<li class="chapter" data-level="15.3" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#compare-the-regressions-side-by-side"><i class="fa fa-check"></i><b>15.3</b> Compare the regressions side-by-side</a></li>
<li class="chapter" data-level="15.4" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#compare-the-predictions-of-each-model"><i class="fa fa-check"></i><b>15.4</b> Compare the predictions of each model</a>
<ul>
<li class="chapter" data-level="15.4.1" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#group-averages"><i class="fa fa-check"></i><b>15.4.1</b> Group averages</a></li>
<li class="chapter" data-level="15.4.2" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#causal-estimates"><i class="fa fa-check"></i><b>15.4.2</b> Causal estimates?</a></li>
<li class="chapter" data-level="15.4.3" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#what-about-age"><i class="fa fa-check"></i><b>15.4.3</b> What about age?</a></li>
<li class="chapter" data-level="15.4.4" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#what-about-sex"><i class="fa fa-check"></i><b>15.4.4</b> What about sex?</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="16" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html"><i class="fa fa-check"></i><b>16</b> Dummy Variables Part 2</a>
<ul>
<li class="chapter" data-level="16.1" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#size-only"><i class="fa fa-check"></i><b>16.1</b> Size only</a></li>
<li class="chapter" data-level="16.2" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#number-of-bathrooms-and-size"><i class="fa fa-check"></i><b>16.2</b> Number of bathrooms and size</a></li>
<li class="chapter" data-level="16.3" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#slope-dummy"><i class="fa fa-check"></i><b>16.3</b> Slope dummy</a></li>
<li class="chapter" data-level="16.4" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#intercept-and-slope-dummies"><i class="fa fa-check"></i><b>16.4</b> Intercept and slope dummies</a></li>
<li class="chapter" data-level="16.5" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#models-with-the-number-of-bedrooms"><i class="fa fa-check"></i><b>16.5</b> Models with the number of bedrooms</a></li>
</ul></li>
<li class="chapter" data-level="17" data-path="fixed-effects.html"><a href="fixed-effects.html"><i class="fa fa-check"></i><b>17</b> Fixed Effects</a>
<ul>
<li class="chapter" data-level="17.1" data-path="fixed-effects.html"><a href="fixed-effects.html#variables"><i class="fa fa-check"></i><b>17.1</b> Variables</a></li>
<li class="chapter" data-level="17.2" data-path="fixed-effects.html"><a href="fixed-effects.html#ols"><i class="fa fa-check"></i><b>17.2</b> OLS</a></li>
<li class="chapter" data-level="17.3" data-path="fixed-effects.html"><a href="fixed-effects.html#country-fixed-effects"><i class="fa fa-check"></i><b>17.3</b> Country Fixed Effects</a></li>
<li class="chapter" data-level="17.4" data-path="fixed-effects.html"><a href="fixed-effects.html#year-fixed-effects"><i class="fa fa-check"></i><b>17.4</b> Year Fixed Effects</a></li>
<li class="chapter" data-level="17.5" data-path="fixed-effects.html"><a href="fixed-effects.html#country-and-year-fixed-effects"><i class="fa fa-check"></i><b>17.5</b> Country and Year Fixed Effects</a></li>
<li class="chapter" data-level="17.6" data-path="fixed-effects.html"><a href="fixed-effects.html#comparison-of-all-models"><i class="fa fa-check"></i><b>17.6</b> Comparison of all models</a>
<ul>
<li class="chapter" data-level="17.6.1" data-path="fixed-effects.html"><a href="fixed-effects.html#within-transformation"><i class="fa fa-check"></i><b>17.6.1</b> Within Transformation</a></li>
<li class="chapter" data-level="17.6.2" data-path="fixed-effects.html"><a href="fixed-effects.html#plm-package"><i class="fa fa-check"></i><b>17.6.2</b> PLM Package</a></li>
<li class="chapter" data-level="17.6.3" data-path="fixed-effects.html"><a href="fixed-effects.html#dummy-variables"><i class="fa fa-check"></i><b>17.6.3</b> Dummy Variables</a></li>
</ul></li>
<li class="chapter" data-level="17.7" data-path="fixed-effects.html"><a href="fixed-effects.html#data-summary-by-country"><i class="fa fa-check"></i><b>17.7</b> Data Summary by Country</a>
<ul>
<li class="chapter" data-level="17.7.1" data-path="fixed-effects.html"><a href="fixed-effects.html#average-values-for-each-country"><i class="fa fa-check"></i><b>17.7.1</b> Average Values for Each Country</a></li>
<li class="chapter" data-level="17.7.2" data-path="fixed-effects.html"><a href="fixed-effects.html#variable-specific-values-and-within-transformation-for-each-country"><i class="fa fa-check"></i><b>17.7.2</b> Variable-Specific Values and Within Transformation for Each Country</a></li>
<li class="chapter" data-level="17.7.3" data-path="fixed-effects.html"><a href="fixed-effects.html#life-expectancy"><i class="fa fa-check"></i><b>17.7.3</b> Life expectancy</a></li>
<li class="chapter" data-level="17.7.4" data-path="fixed-effects.html"><a href="fixed-effects.html#gdp-per-capita"><i class="fa fa-check"></i><b>17.7.4</b> GDP per capita</a></li>
<li class="chapter" data-level="17.7.5" data-path="fixed-effects.html"><a href="fixed-effects.html#population"><i class="fa fa-check"></i><b>17.7.5</b> Population</a></li>
<li class="chapter" data-level="17.7.6" data-path="fixed-effects.html"><a href="fixed-effects.html#percent-female"><i class="fa fa-check"></i><b>17.7.6</b> Percent female</a></li>
<li class="chapter" data-level="17.7.7" data-path="fixed-effects.html"><a href="fixed-effects.html#percent-rural"><i class="fa fa-check"></i><b>17.7.7</b> Percent rural</a></li>
</ul></li>
<li class="chapter" data-level="17.8" data-path="fixed-effects.html"><a href="fixed-effects.html#bookdown-style-note"><i class="fa fa-check"></i><b>17.8</b> Bookdown Style Note</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="difference-in-differences.html"><a href="difference-in-differences.html"><i class="fa fa-check"></i><b>18</b> Difference-in-Differences</a>
<ul>
<li class="chapter" data-level="18.1" data-path="difference-in-differences.html"><a href="difference-in-differences.html#data-1"><i class="fa fa-check"></i><b>18.1</b> Data</a></li>
<li class="chapter" data-level="18.2" data-path="difference-in-differences.html"><a href="difference-in-differences.html#model-1"><i class="fa fa-check"></i><b>18.2</b> Model 1</a>
<ul>
<li class="chapter" data-level="18.2.1" data-path="difference-in-differences.html"><a href="difference-in-differences.html#equivalent-model-1"><i class="fa fa-check"></i><b>18.2.1</b> Equivalent model 1</a></li>
</ul></li>
<li class="chapter" data-level="18.3" data-path="difference-in-differences.html"><a href="difference-in-differences.html#model-2"><i class="fa fa-check"></i><b>18.3</b> Model 2</a></li>
<li class="chapter" data-level="18.4" data-path="difference-in-differences.html"><a href="difference-in-differences.html#comparison-of-models"><i class="fa fa-check"></i><b>18.4</b> Comparison of models</a></li>
<li class="chapter" data-level="18.5" data-path="difference-in-differences.html"><a href="difference-in-differences.html#additional-questions"><i class="fa fa-check"></i><b>18.5</b> Additional questions</a>
<ul>
<li class="chapter" data-level="18.5.1" data-path="difference-in-differences.html"><a href="difference-in-differences.html#question-1"><i class="fa fa-check"></i><b>18.5.1</b> Question 1</a></li>
<li class="chapter" data-level="18.5.2" data-path="difference-in-differences.html"><a href="difference-in-differences.html#question-2"><i class="fa fa-check"></i><b>18.5.2</b> Question 2</a></li>
<li class="chapter" data-level="18.5.3" data-path="difference-in-differences.html"><a href="difference-in-differences.html#question-3"><i class="fa fa-check"></i><b>18.5.3</b> Question 3</a></li>
<li class="chapter" data-level="18.5.4" data-path="difference-in-differences.html"><a href="difference-in-differences.html#question-4"><i class="fa fa-check"></i><b>18.5.4</b> Question 4</a></li>
</ul></li>
<li class="chapter" data-level="18.6" data-path="difference-in-differences.html"><a href="difference-in-differences.html#polynomials"><i class="fa fa-check"></i><b>18.6</b> Polynomials</a></li>
</ul></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">R Programming Guidebook Project</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="cleaning-data-in-r" class="section level1 hasAnchor" number="9">
<h1><span class="header-section-number">9</span> Cleaning Data in R<a href="cleaning-data-in-r.html#cleaning-data-in-r" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p><a href="https://learn.datacamp.com/courses/cleaning-data-in-r" class="uri">https://learn.datacamp.com/courses/cleaning-data-in-r</a></p>
<div id="common-data-problems" class="section level2 hasAnchor" number="9.1">
<h2><span class="header-section-number">9.1</span> Common Data Problems<a href="cleaning-data-in-r.html#common-data-problems" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><strong>Converting data types</strong></p>
<p>Before beginning to analyze any dataset, it’s important to take a look at the different types of columns from the. do that by using <code>glimpse()</code>:</p>
<div class="sourceCode" id="cb449"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb449-1"><a href="cleaning-data-in-r.html#cb449-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Glimpse at bike_share_rides</span></span>
<span id="cb449-2"><a href="cleaning-data-in-r.html#cb449-2" aria-hidden="true" tabindex="-1"></a><span class="fu">glimpse</span>(bike_share_rides)</span></code></pre></div>
<pre><code>## Rows: 35,229
## Columns: 10
## $ ride_id <int> 52797, 54540, 87695, 45619, 70832, 96135, 29928, 83331…
## $ date <chr> "2017-04-15", "2017-04-19", "2017-04-14", "2017-04-03"…
## $ duration <chr> "1316.15 minutes", "8.13 minutes", "24.85 minutes", "6…
## $ station_A_id <dbl> 67, 21, 16, 58, 16, 6, 5, 16, 5, 81, 30, 16, 16, 67, 2…
## $ station_A_name <chr> "San Francisco Caltrain Station 2 (Townsend St at 4th…
## $ station_B_id <dbl> 89, 64, 355, 368, 81, 66, 350, 91, 62, 81, 109, 10, 80…
## $ station_B_name <chr> "Division St at Potrero Ave", "5th St at Brannan St", …
## $ bike_id <dbl> 1974, 860, 2263, 1417, 507, 75, 388, 239, 1449, 3289, …
## $ user_gender <chr> "Male", "Male", "Male", "Male", "Male", "Male", "Male"…
## $ user_birth_year <dbl> 1972, 1986, 1993, 1981, 1981, 1988, 1993, 1996, 1993, …</code></pre>
<div class="sourceCode" id="cb451"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb451-1"><a href="cleaning-data-in-r.html#cb451-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Summary of user_birth_year</span></span>
<span id="cb451-2"><a href="cleaning-data-in-r.html#cb451-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(bike_share_rides<span class="sc">$</span>user_birth_year)</span></code></pre></div>
<pre><code>## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1900 1979 1986 1984 1991 2001</code></pre>
<p>The summary statistics of <code>user_birth_year</code> don’t seem to offer much useful information about the different birth years because the <code>user_birth_year</code> column is a <code>numeric</code> type and should be converted to a <code>factor</code>.</p>
<p>Use <code>dplyr</code> and <code>assertive</code> packages to convert a column into a factor and assert/confirm whether a column is the type wanted or not.</p>
<p>Use <code>as.___()</code> functions to convert objects to a new data type.</p>
<p>Use <code>assert_is____()</code> functions to confirm an object’s data type.</p>
<div class="sourceCode" id="cb453"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb453-1"><a href="cleaning-data-in-r.html#cb453-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert user_birth_year to factor: user_birth_year_fct</span></span>
<span id="cb453-2"><a href="cleaning-data-in-r.html#cb453-2" aria-hidden="true" tabindex="-1"></a>bike_share_rides <span class="ot"><-</span> bike_share_rides <span class="sc">%>%</span></span>
<span id="cb453-3"><a href="cleaning-data-in-r.html#cb453-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">user_birth_year_fct =</span> <span class="fu">as.factor</span>(user_birth_year))</span></code></pre></div>
<p>If the <code>assert</code> is <code>TRUE</code>, nothing will be outputted:</p>
<div class="sourceCode" id="cb454"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb454-1"><a href="cleaning-data-in-r.html#cb454-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Assert user_birth_year_fct is a factor</span></span>
<span id="cb454-2"><a href="cleaning-data-in-r.html#cb454-2" aria-hidden="true" tabindex="-1"></a><span class="fu">assert_is_factor</span>(bike_share_rides<span class="sc">$</span>user_birth_year_fct)</span></code></pre></div>
<div class="sourceCode" id="cb455"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb455-1"><a href="cleaning-data-in-r.html#cb455-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Summary of user_birth_year_fct</span></span>
<span id="cb455-2"><a href="cleaning-data-in-r.html#cb455-2" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(bike_share_rides<span class="sc">$</span>user_birth_year_fct)</span></code></pre></div>
<pre><code>## 1900 1902 1923 1931 1938 1939 1941 1942 1943 1945 1946 1947 1948 1949 1950 1951
## 1 7 2 23 2 1 3 10 4 16 5 24 9 30 37 25
## 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
## 70 49 65 66 112 62 156 99 196 161 256 237 245 349 225 363
## 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
## 365 331 370 548 529 527 563 601 481 541 775 876 825 1016 1056 1262
## 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
## 1157 1318 1606 1672 2135 1872 2062 1582 1703 1498 1476 1185 813 358 365 348
## 2000 2001
## 473 30</code></pre>
<p><strong>Trimming strings</strong></p>
<p>Another common dirty data problem is having extra bits like percent signs or periods in numbers, causing them to be read in as <code>character</code>.</p>
<p>Use <code>str_remove()</code> to remove <code>"minutes"</code> from the <code>duration</code> column of <code>bike_share_rides.</code> Add this as a new column called <code>duration_trimmed</code>.</p>
<p>Convert the <code>duration_trimmed</code> column to a numeric type and add this as a new column called <code>duration_mins</code>.</p>
<p><code>Glimpse</code> at <code>bike_share_rides</code> and <code>assert</code> that the <code>duration_mins</code> column is <code>numeric</code>.</p>
<div class="sourceCode" id="cb457"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb457-1"><a href="cleaning-data-in-r.html#cb457-1" aria-hidden="true" tabindex="-1"></a>bike_share_rides <span class="ot"><-</span> bike_share_rides <span class="sc">%>%</span></span>
<span id="cb457-2"><a href="cleaning-data-in-r.html#cb457-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Remove 'minutes' from duration: duration_trimmed</span></span>
<span id="cb457-3"><a href="cleaning-data-in-r.html#cb457-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">duration_trimmed =</span> <span class="fu">str_remove</span>(duration, <span class="st">"minutes"</span>),</span>
<span id="cb457-4"><a href="cleaning-data-in-r.html#cb457-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Convert duration_trimmed to numeric: duration_mins</span></span>
<span id="cb457-5"><a href="cleaning-data-in-r.html#cb457-5" aria-hidden="true" tabindex="-1"></a> <span class="at">duration_mins =</span> <span class="fu">as.numeric</span>(duration_trimmed))</span></code></pre></div>
<div class="sourceCode" id="cb458"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb458-1"><a href="cleaning-data-in-r.html#cb458-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Glimpse at bike_share_rides</span></span>
<span id="cb458-2"><a href="cleaning-data-in-r.html#cb458-2" aria-hidden="true" tabindex="-1"></a><span class="fu">glimpse</span>(bike_share_rides)</span></code></pre></div>
<pre><code>## Rows: 35,229
## Columns: 13
## $ ride_id <int> 52797, 54540, 87695, 45619, 70832, 96135, 29928, 8…
## $ date <chr> "2017-04-15", "2017-04-19", "2017-04-14", "2017-04…
## $ duration <chr> "1316.15 minutes", "8.13 minutes", "24.85 minutes"…
## $ station_A_id <dbl> 67, 21, 16, 58, 16, 6, 5, 16, 5, 81, 30, 16, 16, 6…
## $ station_A_name <chr> "San Francisco Caltrain Station 2 (Townsend St at…
## $ station_B_id <dbl> 89, 64, 355, 368, 81, 66, 350, 91, 62, 81, 109, 10…
## $ station_B_name <chr> "Division St at Potrero Ave", "5th St at Brannan S…
## $ bike_id <dbl> 1974, 860, 2263, 1417, 507, 75, 388, 239, 1449, 32…
## $ user_gender <chr> "Male", "Male", "Male", "Male", "Male", "Male", "M…
## $ user_birth_year <dbl> 1972, 1986, 1993, 1981, 1981, 1988, 1993, 1996, 19…
## $ user_birth_year_fct <fct> 1972, 1986, 1993, 1981, 1981, 1988, 1993, 1996, 19…
## $ duration_trimmed <chr> "1316.15 ", "8.13 ", "24.85 ", "6.35 ", "9.8 ", "1…
## $ duration_mins <dbl> 1316.15, 8.13, 24.85, 6.35, 9.80, 17.47, 16.52, 14…</code></pre>
<div class="sourceCode" id="cb460"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb460-1"><a href="cleaning-data-in-r.html#cb460-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Assert duration_mins is numeric</span></span>
<span id="cb460-2"><a href="cleaning-data-in-r.html#cb460-2" aria-hidden="true" tabindex="-1"></a><span class="fu">assert_is_numeric</span>(bike_share_rides<span class="sc">$</span>duration_mins)</span></code></pre></div>
<p>For more details, go to the <em>String Wrangling</em> section at the bottom of <a href="https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/working-with-data-in-the-tidyverse.html#transform-your-data">Transform your data</a> chapter of <em>Working with Data in the Tidyverse</em>.</p>
<p><strong>Range constraints</strong></p>
<center>
<strong>Time range</strong>
</center>
<p>Values that are out of range can throw off an analysis, so it’s important to catch them early on.</p>
<p>examine the <code>duration_min</code> column: Bikes are not allowed to be kept out more than 24 hours/1440 minutes at a time, but issues with some of the bikes caused inaccurate recording of the time they were returned.</p>
<p>Create a three-bin histogram of the <code>duration_min</code> column of <code>bike_share_rides</code> using <code>ggplot2</code> to identify if there is out-of-range data.</p>
<p>Replace the values of <code>duration_min</code> that are greater than <code>1440</code> minutes (24 hours) with <code>1440.</code> Add this to <code>bike_share_rides</code> as a new column called <code>duration_min_const</code>.</p>
<p>Assert that all values of <code>duration_min_const</code> are between <code>0</code> and <code>1440</code>:</p>
<div class="sourceCode" id="cb461"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb461-1"><a href="cleaning-data-in-r.html#cb461-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Create breaks</span></span>
<span id="cb461-2"><a href="cleaning-data-in-r.html#cb461-2" aria-hidden="true" tabindex="-1"></a>breaks <span class="ot"><-</span> <span class="fu">c</span>(<span class="fu">min</span>(bike_share_rides<span class="sc">$</span>duration_mins), <span class="dv">0</span>, <span class="dv">1440</span>, <span class="fu">max</span>(bike_share_rides<span class="sc">$</span>duration_mins))</span>
<span id="cb461-3"><a href="cleaning-data-in-r.html#cb461-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb461-4"><a href="cleaning-data-in-r.html#cb461-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Create a histogram of duration_min</span></span>
<span id="cb461-5"><a href="cleaning-data-in-r.html#cb461-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(bike_share_rides, <span class="fu">aes</span>(duration_mins)) <span class="sc">+</span></span>
<span id="cb461-6"><a href="cleaning-data-in-r.html#cb461-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_histogram</span>(<span class="at">breaks =</span> breaks)</span></code></pre></div>
<p><img src="09-Cleaning_Data_in_R_files/figure-html/unnamed-chunk-9-1.png" width="672" /></p>
<div class="sourceCode" id="cb462"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb462-1"><a href="cleaning-data-in-r.html#cb462-1" aria-hidden="true" tabindex="-1"></a><span class="co"># duration_min_const: replace vals of duration_min > 1440 with 1440</span></span>
<span id="cb462-2"><a href="cleaning-data-in-r.html#cb462-2" aria-hidden="true" tabindex="-1"></a>bike_share_rides <span class="ot"><-</span> bike_share_rides <span class="sc">%>%</span></span>
<span id="cb462-3"><a href="cleaning-data-in-r.html#cb462-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">duration_min_const =</span> <span class="fu">replace</span>(duration_mins, duration_mins <span class="sc">></span> <span class="dv">1440</span>, <span class="dv">1440</span>))</span>
<span id="cb462-4"><a href="cleaning-data-in-r.html#cb462-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb462-5"><a href="cleaning-data-in-r.html#cb462-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Make sure all values of duration_min_const are between 0 and 1440</span></span>
<span id="cb462-6"><a href="cleaning-data-in-r.html#cb462-6" aria-hidden="true" tabindex="-1"></a><span class="fu">assert_all_are_in_closed_range</span>(bike_share_rides<span class="sc">$</span>duration_min_const, <span class="at">lower =</span> <span class="dv">0</span>, <span class="at">upper =</span> <span class="dv">1440</span>)</span></code></pre></div>
<center>
<strong>Date range</strong>
</center>
<p>Something has gone wrong and there are data with dates from the future, which is way outside of the date range to be working with. To fix this, remove any rides from the dataset that have a date in the future.</p>
<p>Convert the <code>date</code> column of <code>bike_share_rides</code> from <code>character</code> to the <code>Date</code> data type.</p>
<p><code>Assert</code> that all values in the <code>date</code> column happened sometime in the past and not in the future.</p>
<div class="sourceCode" id="cb463"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb463-1"><a href="cleaning-data-in-r.html#cb463-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert date to Date type</span></span>
<span id="cb463-2"><a href="cleaning-data-in-r.html#cb463-2" aria-hidden="true" tabindex="-1"></a>bike_share_rides <span class="ot"><-</span> bike_share_rides <span class="sc">%>%</span></span>
<span id="cb463-3"><a href="cleaning-data-in-r.html#cb463-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date =</span> <span class="fu">as.Date</span>(date))</span>
<span id="cb463-4"><a href="cleaning-data-in-r.html#cb463-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb463-5"><a href="cleaning-data-in-r.html#cb463-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Make sure all dates are in the past</span></span>
<span id="cb463-6"><a href="cleaning-data-in-r.html#cb463-6" aria-hidden="true" tabindex="-1"></a><span class="fu">assert_all_are_in_past</span>(bike_share_rides<span class="sc">$</span>date)</span></code></pre></div>
<pre><code>## Warning: Coercing bike_share_rides$date to class 'POSIXct'.</code></pre>
<p>Filter <code>bike_share_rides</code> to get only the rides from the past or today, and save this as <code>bike_share_rides_past.</code></p>
<p><code>Assert</code> that the dates in <code>bike_share_rides_past</code> occurred only in the past.</p>
<div class="sourceCode" id="cb465"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb465-1"><a href="cleaning-data-in-r.html#cb465-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Filter for rides that occurred before or on today's date</span></span>
<span id="cb465-2"><a href="cleaning-data-in-r.html#cb465-2" aria-hidden="true" tabindex="-1"></a>bike_share_rides_past <span class="ot"><-</span> bike_share_rides <span class="sc">%>%</span></span>
<span id="cb465-3"><a href="cleaning-data-in-r.html#cb465-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(date <span class="sc"><=</span> <span class="fu">today</span>())</span>
<span id="cb465-4"><a href="cleaning-data-in-r.html#cb465-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb465-5"><a href="cleaning-data-in-r.html#cb465-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Make sure all dates from bike_share_rides_past are in the past</span></span>
<span id="cb465-6"><a href="cleaning-data-in-r.html#cb465-6" aria-hidden="true" tabindex="-1"></a><span class="fu">assert_all_are_in_past</span>(bike_share_rides_past<span class="sc">$</span>date)</span></code></pre></div>
<pre><code>## Warning: Coercing bike_share_rides_past$date to class 'POSIXct'.</code></pre>
<p><strong>Uniqueness constraints</strong></p>
<center>
<strong>Full duplicates</strong>
</center>
<p>When multiple rows of a data frame share the same values for all columns, they’re full duplicates of each other. Removing duplicates like this is important, since having the same value repeated multiple times can alter summary statistics like the <code>mean</code> and <code>median.</code></p>
<p>Get the total number of full duplicates in <code>bike_share_rides</code>.</p>
<p>Remove all full duplicates from <code>bike_share_rides</code> and save the new data frame as <code>bike_share_rides_unique</code>.</p>
<p>Get the total number of full duplicates in the new <code>bike_share_rides_unique</code> data frame.</p>
<div class="sourceCode" id="cb467"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb467-1"><a href="cleaning-data-in-r.html#cb467-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Count the number of full duplicates</span></span>
<span id="cb467-2"><a href="cleaning-data-in-r.html#cb467-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">duplicated</span>(bike_share_rides))</span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<div class="sourceCode" id="cb469"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb469-1"><a href="cleaning-data-in-r.html#cb469-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Remove duplicates</span></span>
<span id="cb469-2"><a href="cleaning-data-in-r.html#cb469-2" aria-hidden="true" tabindex="-1"></a>bike_share_rides_unique <span class="ot"><-</span> <span class="fu">distinct</span>(bike_share_rides)</span>
<span id="cb469-3"><a href="cleaning-data-in-r.html#cb469-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb469-4"><a href="cleaning-data-in-r.html#cb469-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Count the full duplicates in bike_share_rides_unique</span></span>
<span id="cb469-5"><a href="cleaning-data-in-r.html#cb469-5" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">duplicated</span>(bike_share_rides_unique))</span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<center>
<strong>Partial duplicates</strong>
</center>
<p>Identify any partial duplicates and then practice the most common technique to deal with them, which involves dropping all partial duplicates, keeping only the first.</p>
<p>Remove full and partial duplicates from <code>bike_share_rides</code> based on <code>ride_id</code> only, keeping all columns. Store this as <code>bike_share_rides_unique</code>.</p>
<div class="sourceCode" id="cb471"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb471-1"><a href="cleaning-data-in-r.html#cb471-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Remove full and partial duplicates</span></span>
<span id="cb471-2"><a href="cleaning-data-in-r.html#cb471-2" aria-hidden="true" tabindex="-1"></a>bike_share_rides_unique <span class="ot"><-</span> bike_share_rides <span class="sc">%>%</span></span>
<span id="cb471-3"><a href="cleaning-data-in-r.html#cb471-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># Only based on ride_id instead of all cols</span></span>
<span id="cb471-4"><a href="cleaning-data-in-r.html#cb471-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">distinct</span>(ride_id, <span class="at">.keep_all =</span> <span class="cn">TRUE</span>)</span>
<span id="cb471-5"><a href="cleaning-data-in-r.html#cb471-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb471-6"><a href="cleaning-data-in-r.html#cb471-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Find duplicated ride_ids in bike_share_rides_unique</span></span>
<span id="cb471-7"><a href="cleaning-data-in-r.html#cb471-7" aria-hidden="true" tabindex="-1"></a>bike_share_rides_unique <span class="sc">%>%</span></span>
<span id="cb471-8"><a href="cleaning-data-in-r.html#cb471-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># Count the number of occurrences of each ride_id</span></span>
<span id="cb471-9"><a href="cleaning-data-in-r.html#cb471-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(ride_id) <span class="sc">%>%</span></span>
<span id="cb471-10"><a href="cleaning-data-in-r.html#cb471-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># Filter for rows with a count > 1</span></span>
<span id="cb471-11"><a href="cleaning-data-in-r.html#cb471-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(n <span class="sc">></span> <span class="dv">1</span>)</span></code></pre></div>
<pre><code>## # A tibble: 0 × 2
## # … with 2 variables: ride_id <int>, n <int></code></pre>
<p><strong>Aggregating partial duplicates</strong></p>
<p>Another way of handling partial duplicates is to compute a summary statistic of the values that differ between partial duplicates, such as <code>mean</code>, <code>median</code>, <code>maximum</code>, or <code>minimum.</code> This can come in handy when you’re not sure how your data was collected and want an average, or if based on domain knowledge, you’d rather have too high of an estimate than too low of an estimate (or vice versa).</p>
<div class="sourceCode" id="cb473"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb473-1"><a href="cleaning-data-in-r.html#cb473-1" aria-hidden="true" tabindex="-1"></a>bike_share_rides <span class="sc">%>%</span></span>
<span id="cb473-2"><a href="cleaning-data-in-r.html#cb473-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Group by ride_id and date</span></span>
<span id="cb473-3"><a href="cleaning-data-in-r.html#cb473-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(ride_id, date) <span class="sc">%>%</span></span>
<span id="cb473-4"><a href="cleaning-data-in-r.html#cb473-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Add duration_min_avg column</span></span>
<span id="cb473-5"><a href="cleaning-data-in-r.html#cb473-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">duration_min_avg =</span> <span class="fu">mean</span>(duration_mins)) <span class="sc">%>%</span></span>
<span id="cb473-6"><a href="cleaning-data-in-r.html#cb473-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># Remove duplicates based on ride_id and date, keep all cols</span></span>
<span id="cb473-7"><a href="cleaning-data-in-r.html#cb473-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">distinct</span>(ride_id, date, <span class="at">.keep_all =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span></span>
<span id="cb473-8"><a href="cleaning-data-in-r.html#cb473-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># Remove duration_min column</span></span>
<span id="cb473-9"><a href="cleaning-data-in-r.html#cb473-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="sc">-</span>duration_mins)</span></code></pre></div>
<pre><code>## # A tibble: 35,229 × 14
## # Groups: ride_id, date [35,229]
## ride_id date duration station_A_id station_A_name station_B_id
## <int> <date> <chr> <dbl> <chr> <dbl>
## 1 52797 2017-04-15 1316.15 minutes 67 San Francisco C… 89
## 2 54540 2017-04-19 8.13 minutes 21 Montgomery St B… 64
## 3 87695 2017-04-14 24.85 minutes 16 Steuart St at M… 355
## 4 45619 2017-04-03 6.35 minutes 58 Market St at 10… 368
## 5 70832 2017-04-10 9.8 minutes 16 Steuart St at M… 81
## 6 96135 2017-04-18 17.47 minutes 6 The Embarcadero… 66
## 7 29928 2017-04-22 16.52 minutes 5 Powell St BART … 350
## 8 83331 2017-04-11 14.72 minutes 16 Steuart St at M… 91
## 9 72424 2017-04-05 4.12 minutes 5 Powell St BART … 62
## 10 25910 2017-04-20 25.77 minutes 81 Berry St at 4th… 81
## # … with 35,219 more rows, and 8 more variables: station_B_name <chr>,
## # bike_id <dbl>, user_gender <chr>, user_birth_year <dbl>,
## # user_birth_year_fct <fct>, duration_trimmed <chr>,
## # duration_min_const <dbl>, duration_min_avg <dbl></code></pre>
</div>
<div id="categorical-and-text-data" class="section level2 hasAnchor" number="9.2">
<h2><span class="header-section-number">9.2</span> Categorical and Text Data<a href="cleaning-data-in-r.html#categorical-and-text-data" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><strong>Membership data range</strong></p>
<p>A categorical data column would sometime have a limited range of observations that can be classified into membership list. Observations that doesn’t belong to this membership are outliers, and wouldn’t make sense.</p>
<p><code>Count</code> the number of occurrences of each <code>dest_size</code> in <code>sfo_survey</code>.</p>
<p><code>"huge"</code>, <code>" Small "</code>, <code>"Large "</code>, and <code>" Hub"</code> appear to violate membership constraints.</p>
<div class="sourceCode" id="cb475"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb475-1"><a href="cleaning-data-in-r.html#cb475-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Count the number of occurrences of dest_size</span></span>
<span id="cb475-2"><a href="cleaning-data-in-r.html#cb475-2" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span></span>
<span id="cb475-3"><a href="cleaning-data-in-r.html#cb475-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(dest_size)</span></code></pre></div>
<pre><code>## dest_size n
## 1 Small 1
## 2 Hub 1
## 3 Hub 1756
## 4 Large 143
## 5 Large 1
## 6 Medium 682
## 7 Small 225</code></pre>
<p>Use the correct filtering join on <code>sfo_survey</code> and <code>dest_sizes</code> to get the rows of <code>sfo_survey</code> that have a valid <code>dest_size</code>:</p>
<div class="sourceCode" id="cb477"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb477-1"><a href="cleaning-data-in-r.html#cb477-1" aria-hidden="true" tabindex="-1"></a>dest_sizes <span class="ot"><-</span> <span class="fu">structure</span>(<span class="fu">list</span>(<span class="at">dest_size =</span> <span class="fu">c</span>(<span class="st">"Small"</span>, <span class="st">"Medium"</span>, <span class="st">"Large"</span>, <span class="st">"Hub"</span></span>
<span id="cb477-2"><a href="cleaning-data-in-r.html#cb477-2" aria-hidden="true" tabindex="-1"></a>), <span class="at">passengers_per_day =</span> <span class="fu">structure</span>(<span class="fu">c</span>(1L, 3L, 4L, 2L), <span class="at">.Label =</span> <span class="fu">c</span>(<span class="st">"0-20K"</span>, </span>
<span id="cb477-3"><a href="cleaning-data-in-r.html#cb477-3" aria-hidden="true" tabindex="-1"></a><span class="st">"100K+"</span>, <span class="st">"20K-70K"</span>, <span class="st">"70K-100K"</span>), <span class="at">class =</span> <span class="st">"factor"</span>)), <span class="at">.Names =</span> <span class="fu">c</span>(<span class="st">"dest_size"</span>, </span>
<span id="cb477-4"><a href="cleaning-data-in-r.html#cb477-4" aria-hidden="true" tabindex="-1"></a><span class="st">"passengers_per_day"</span>), <span class="at">row.names =</span> <span class="fu">c</span>(<span class="cn">NA</span>, <span class="sc">-</span>4L), <span class="at">class =</span> <span class="st">"data.frame"</span>)</span></code></pre></div>
<div class="sourceCode" id="cb478"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb478-1"><a href="cleaning-data-in-r.html#cb478-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Remove bad dest_size rows</span></span>
<span id="cb478-2"><a href="cleaning-data-in-r.html#cb478-2" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span> </span>
<span id="cb478-3"><a href="cleaning-data-in-r.html#cb478-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># Join with dest_sizes</span></span>
<span id="cb478-4"><a href="cleaning-data-in-r.html#cb478-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">semi_join</span>(dest_sizes, <span class="at">by =</span> <span class="st">"dest_size"</span>)<span class="sc">%>%</span></span>
<span id="cb478-5"><a href="cleaning-data-in-r.html#cb478-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># Count the number of each dest_size</span></span>
<span id="cb478-6"><a href="cleaning-data-in-r.html#cb478-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(dest_size)</span></code></pre></div>
<pre><code>## dest_size n
## 1 Hub 1756
## 2 Large 143
## 3 Medium 682
## 4 Small 225</code></pre>
<p><strong>Identifying inconsistency</strong></p>
<p>Sometimes, there are different kinds of inconsistencies that can occur within categories, making it look like a variable has more categories than it should.</p>
<p>Examine the <code>dest_size</code> column again as well as the <code>cleanliness</code> column and determine what kind of issues, if any, these two categorical variables face.</p>
<p>Count the number of occurrences of each category of the <code>dest_size</code> variable of <code>sfo_survey</code>. The categories in <code>dest_size</code> have <strong>inconsistent white space</strong>:</p>
<div class="sourceCode" id="cb480"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb480-1"><a href="cleaning-data-in-r.html#cb480-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Count dest_size</span></span>
<span id="cb480-2"><a href="cleaning-data-in-r.html#cb480-2" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span></span>
<span id="cb480-3"><a href="cleaning-data-in-r.html#cb480-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(dest_size)</span></code></pre></div>
<pre><code>## dest_size n
## 1 Small 1
## 2 Hub 1
## 3 Hub 1756
## 4 Large 143
## 5 Large 1
## 6 Medium 682
## 7 Small 225</code></pre>
<p>Count the number of occurrences of each category of the <code>cleanliness</code> variable of <code>sfo_survey</code>. The categories in <code>cleanliness</code> have <strong>inconsistent capitalization</strong>.</p>
<div class="sourceCode" id="cb482"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb482-1"><a href="cleaning-data-in-r.html#cb482-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Count cleanliness</span></span>
<span id="cb482-2"><a href="cleaning-data-in-r.html#cb482-2" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span></span>
<span id="cb482-3"><a href="cleaning-data-in-r.html#cb482-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(cleanliness)</span></code></pre></div>
<pre><code>## cleanliness n
## 1 Average 433
## 2 Clean 970
## 3 Dirty 2
## 4 Somewhat clean 1254
## 5 Somewhat dirty 30
## 6 <NA> 120</code></pre>
<p><strong>Correcting inconsistency</strong></p>
<p><code>dest_size</code> has whitespace inconsistencies and cleanliness has capitalization inconsistencies, use the new tools to fix the inconsistent values in <code>sfo_survey</code> instead of removing the data points entirely.</p>
<p>Add a column to <code>sfo_survey</code> called <code>dest_size_trimmed</code> that contains the values in the <code>dest_size</code> column with all leading and trailing whitespace removed.</p>
<p>Add another column called <code>cleanliness_lower</code> that contains the values in the <code>cleanliness</code> column converted to all lowercase.</p>
<div class="sourceCode" id="cb484"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb484-1"><a href="cleaning-data-in-r.html#cb484-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Add new columns to sfo_survey</span></span>
<span id="cb484-2"><a href="cleaning-data-in-r.html#cb484-2" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="ot"><-</span> sfo_survey <span class="sc">%>%</span></span>
<span id="cb484-3"><a href="cleaning-data-in-r.html#cb484-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># dest_size_trimmed: dest_size without whitespace</span></span>
<span id="cb484-4"><a href="cleaning-data-in-r.html#cb484-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">dest_size_trimmed =</span> <span class="fu">str_trim</span>(dest_size),</span>
<span id="cb484-5"><a href="cleaning-data-in-r.html#cb484-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># cleanliness_lower: cleanliness converted to lowercase</span></span>
<span id="cb484-6"><a href="cleaning-data-in-r.html#cb484-6" aria-hidden="true" tabindex="-1"></a> <span class="at">cleanliness_lower =</span> <span class="fu">str_to_lower</span>(cleanliness))</span>
<span id="cb484-7"><a href="cleaning-data-in-r.html#cb484-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb484-8"><a href="cleaning-data-in-r.html#cb484-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Count values of dest_size_trimmed</span></span>
<span id="cb484-9"><a href="cleaning-data-in-r.html#cb484-9" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span></span>
<span id="cb484-10"><a href="cleaning-data-in-r.html#cb484-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(dest_size_trimmed)</span></code></pre></div>
<pre><code>## dest_size_trimmed n
## 1 Hub 1757
## 2 Large 144
## 3 Medium 682
## 4 Small 226</code></pre>
<div class="sourceCode" id="cb486"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb486-1"><a href="cleaning-data-in-r.html#cb486-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Count values of cleanliness_lower</span></span>
<span id="cb486-2"><a href="cleaning-data-in-r.html#cb486-2" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span></span>
<span id="cb486-3"><a href="cleaning-data-in-r.html#cb486-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(cleanliness_lower)</span></code></pre></div>
<pre><code>## cleanliness_lower n
## 1 average 433
## 2 clean 970
## 3 dirty 2
## 4 somewhat clean 1254
## 5 somewhat dirty 30
## 6 <NA> 120</code></pre>
<p><strong>Collapsing categories</strong></p>
<p>Sometimes, there are observations that have input error that make it slightly different from the group it should belong to. Collapse(merge, or cover the error over with an umbrella group) to simply, fix the variable:</p>
<div class="sourceCode" id="cb488"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb488-1"><a href="cleaning-data-in-r.html#cb488-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Count categories of dest_region</span></span>
<span id="cb488-2"><a href="cleaning-data-in-r.html#cb488-2" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span></span>
<span id="cb488-3"><a href="cleaning-data-in-r.html#cb488-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(dest_region)</span></code></pre></div>
<pre><code>## dest_region n
## 1 Asia 260
## 2 Australia/New Zealand 66
## 3 Canada/Mexico 220
## 4 Central/South America 29
## 5 East US 498
## 6 Europe 401
## 7 Middle East 79
## 8 Midwest US 281
## 9 West US 975</code></pre>
<p><code>"EU"</code>, <code>"eur"</code>, and <code>"Europ"</code> need to be collapsed to <code>"Europe"</code>.</p>
<p>Create a vector called <code>europe_categories</code> containing the three values of <code>dest_region</code> that need to be collapsed.</p>
<p>Add a new column to <code>sfo_survey</code> called <code>dest_region_collapsed</code> that contains the values from the <code>dest_region</code> column, except the categories stored in <code>europe_categories</code> should be collapsed to Europe.</p>
<div class="sourceCode" id="cb490"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb490-1"><a href="cleaning-data-in-r.html#cb490-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Count categories of dest_region</span></span>
<span id="cb490-2"><a href="cleaning-data-in-r.html#cb490-2" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span></span>
<span id="cb490-3"><a href="cleaning-data-in-r.html#cb490-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(dest_region)</span></code></pre></div>
<pre><code>## dest_region n
## 1 Asia 260
## 2 Australia/New Zealand 66
## 3 Canada/Mexico 220
## 4 Central/South America 29
## 5 East US 498
## 6 Europe 401
## 7 Middle East 79
## 8 Midwest US 281
## 9 West US 975</code></pre>
<div class="sourceCode" id="cb492"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb492-1"><a href="cleaning-data-in-r.html#cb492-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Categories to map to Europe</span></span>
<span id="cb492-2"><a href="cleaning-data-in-r.html#cb492-2" aria-hidden="true" tabindex="-1"></a>europe_categories <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Europ"</span>, <span class="st">"eur"</span>, <span class="st">"EU"</span>)</span>
<span id="cb492-3"><a href="cleaning-data-in-r.html#cb492-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb492-4"><a href="cleaning-data-in-r.html#cb492-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Add a new col dest_region_collapsed</span></span>
<span id="cb492-5"><a href="cleaning-data-in-r.html#cb492-5" aria-hidden="true" tabindex="-1"></a>sfo_survey <span class="sc">%>%</span></span>
<span id="cb492-6"><a href="cleaning-data-in-r.html#cb492-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># Map all categories in europe_categories to Europe</span></span>
<span id="cb492-7"><a href="cleaning-data-in-r.html#cb492-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">dest_region_collapsed =</span> <span class="fu">fct_collapse</span>(dest_region, </span>
<span id="cb492-8"><a href="cleaning-data-in-r.html#cb492-8" aria-hidden="true" tabindex="-1"></a> <span class="at">Europe =</span> europe_categories)) <span class="sc">%>%</span></span>
<span id="cb492-9"><a href="cleaning-data-in-r.html#cb492-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Count categories of dest_region_collapsed</span></span>
<span id="cb492-10"><a href="cleaning-data-in-r.html#cb492-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(dest_region_collapsed)</span></code></pre></div>
<pre><code>## Warning: Unknown levels in `f`: Europ, eur, EU</code></pre>
<pre><code>## dest_region_collapsed n
## 1 Asia 260
## 2 Australia/New Zealand 66
## 3 Canada/Mexico 220
## 4 Central/South America 29
## 5 East US 498
## 6 Europe 401
## 7 Middle East 79
## 8 Midwest US 281
## 9 West US 975</code></pre>
<p>For more details, go to the <em>(How To Collapse/Merge Levels)</em> section of <a href="https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/categorical-data-in-the-tidyverse.html#manipulating-factor-variables">Manipulating Factor Variables</a> chapter of <em>Categorical Data in the Tidyverse</em>.</p>
<p><strong>Detecting inconsistent text data</strong></p>
<p>Sometimes, in a column, there are inconsistent observations in different formats.</p>
<p>Filter for rows with phone numbers that contain <code>"("</code>, or <code>")"</code>. Remember to use <code>fixed()</code> when searching for parentheses.</p>
<div class="sourceCode" id="cb495"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb495-1"><a href="cleaning-data-in-r.html#cb495-1" aria-hidden="true" tabindex="-1"></a>sfo_survey[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,] <span class="sc">%>%</span></span>
<span id="cb495-2"><a href="cleaning-data-in-r.html#cb495-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="fu">str_detect</span>(safety, <span class="st">"safe"</span>) <span class="sc">|</span> <span class="fu">str_detect</span>(safety, <span class="st">"danger"</span>))</span></code></pre></div>
<pre><code>## id day airline destination dest_region dest_size
## 1 1844 Monday TURKISH AIRLINES ISTANBUL Middle East Hub
## 2 1840 Monday TURKISH AIRLINES ISTANBUL Middle East Hub
## 3 1837 Monday TURKISH AIRLINES ISTANBUL Middle East Hub
## 4 3010 Wednesday AMERICAN MIAMI East US Hub
## 5 1838 Monday TURKISH AIRLINES ISTANBUL Middle East Hub
## 6 1845 Monday TURKISH AIRLINES ISTANBUL Middle East Hub
## 7 2097 Monday UNITED INTL MEXICO CITY Canada/Mexico Hub
## 8 1846 Monday TURKISH AIRLINES ISTANBUL Middle East Hub
## boarding_area dept_time wait_min cleanliness safety
## 1 Gates 91-102 2018-12-31 315 Somewhat clean Somewhat safe
## 2 Gates 91-102 2018-12-31 165 Average Somewhat safe
## 3 Gates 91-102 2018-12-31 225 Somewhat clean Somewhat safe
## 4 Gates 50-59 2018-12-31 88 Somewhat clean Very safe
## 5 Gates 91-102 2018-12-31 195 Somewhat clean Very safe
## 6 Gates 91-102 2018-12-31 135 Average Somewhat safe
## 7 Gates 91-102 2018-12-31 145 Somewhat clean Somewhat safe
## 8 Gates 91-102 2018-12-31 145 Clean Somewhat safe
## satisfaction dest_size_trimmed cleanliness_lower
## 1 Somewhat satsified Hub somewhat clean
## 2 Somewhat satsified Hub average
## 3 Somewhat satsified Hub somewhat clean
## 4 Somewhat satsified Hub somewhat clean
## 5 Somewhat satsified Hub somewhat clean
## 6 Somewhat satsified Hub average
## 7 Somewhat satsified Hub somewhat clean
## 8 Somewhat satsified Hub clean</code></pre>
<p>For more details, go to the <em>String Wrangling</em> section at the bottom of <a href="https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/working-with-data-in-the-tidyverse.html#transform-your-data">Transform your data</a> chapter of <em>Working with Data in the Tidyverse</em>.</p>
<p><strong>Replacing and removing</strong></p>
<p>The <code>str_remove_all()</code> function will remove all instances of the string passed to it.</p>
<div class="sourceCode" id="cb497"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb497-1"><a href="cleaning-data-in-r.html#cb497-1" aria-hidden="true" tabindex="-1"></a>sfo_survey[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,] <span class="sc">%>%</span></span>
<span id="cb497-2"><a href="cleaning-data-in-r.html#cb497-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">safe_or_not =</span> <span class="fu">str_remove_all</span>(safety, <span class="st">"Somewhat"</span>)) <span class="sc">%>%</span></span>
<span id="cb497-3"><a href="cleaning-data-in-r.html#cb497-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(airline, safe_or_not)</span></code></pre></div>
<pre><code>## airline safe_or_not
## 1 TURKISH AIRLINES Neutral
## 2 TURKISH AIRLINES safe
## 3 TURKISH AIRLINES safe
## 4 TURKISH AIRLINES safe
## 5 TURKISH AIRLINES Neutral
## 6 AMERICAN Very safe
## 7 TURKISH AIRLINES Very safe
## 8 TURKISH AIRLINES safe
## 9 UNITED INTL safe
## 10 TURKISH AIRLINES safe</code></pre>
<p>Again, go to the <em>String Wrangling</em> section at the bottom of <a href="https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/working-with-data-in-the-tidyverse.html#transform-your-data">Transform your data</a></p>
<p><strong>Filter/select observations with certain length</strong></p>
<p>The <code>str_length()</code> function takes in a character vector, returns a number for each element that indicates the length of each element.</p>
<div class="sourceCode" id="cb499"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb499-1"><a href="cleaning-data-in-r.html#cb499-1" aria-hidden="true" tabindex="-1"></a>clean_only <span class="ot"><-</span> sfo_survey <span class="sc">%>%</span></span>
<span id="cb499-2"><a href="cleaning-data-in-r.html#cb499-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="fu">str_length</span>(cleanliness_lower) <span class="sc">==</span> <span class="dv">5</span>)</span>
<span id="cb499-3"><a href="cleaning-data-in-r.html#cb499-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb499-4"><a href="cleaning-data-in-r.html#cb499-4" aria-hidden="true" tabindex="-1"></a>clean_only[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,] <span class="sc">%>%</span></span>
<span id="cb499-5"><a href="cleaning-data-in-r.html#cb499-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(airline, cleanliness_lower)</span></code></pre></div>
<pre><code>## airline cleanliness_lower
## 1 TURKISH AIRLINES clean
## 2 TURKISH AIRLINES clean
## 3 TURKISH AIRLINES clean
## 4 TURKISH AIRLINES clean
## 5 TURKISH AIRLINES clean
## 6 TURKISH AIRLINES clean
## 7 CATHAY PACIFIC clean
## 8 UNITED clean
## 9 UNITED clean
## 10 FRONTIER clean</code></pre>
</div>
<div id="advanced-data-problems" class="section level2 hasAnchor" number="9.3">
<h2><span class="header-section-number">9.3</span> Advanced Data Problems<a href="cleaning-data-in-r.html#advanced-data-problems" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><strong>Date uniformity</strong></p>
<p>Make sure that the <code>accounts</code> dataset doesn’t contain any uniformity problems. In this exercise, investigate the <code>date_opened</code> column and clean it up so that all the dates are in the same format.</p>
<p>By default, <code>as.Date()</code> can’t convert <code>"Month DD, YYYY"</code> formats:</p>
<div class="sourceCode" id="cb501"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb501-1"><a href="cleaning-data-in-r.html#cb501-1" aria-hidden="true" tabindex="-1"></a><span class="fu">as.Date</span>(accounts<span class="sc">$</span>date_opened)</span></code></pre></div>
<pre><code>## [1] "2003-10-19" NA "2008-07-29" "2005-06-09" "2012-03-31"
## [6] "2007-06-20" NA "2019-06-03" "2011-05-07" "2018-04-07"
## [11] "2018-11-16" "2001-04-16" "2005-04-21" "2006-06-13" "2009-01-07"
## [16] "2012-07-07" NA NA "2004-05-21" "2001-09-06"
## [21] "2005-04-09" "2009-10-20" "2003-05-16" "2015-10-25" NA
## [26] NA NA "2008-12-27" "2015-11-11" "2009-02-26"
## [31] "2008-12-26" NA NA "2005-12-13" NA
## [36] "2004-12-03" "2016-10-19" NA "2009-10-05" "2013-07-11"
## [41] "2002-03-24" "2015-10-17" NA NA "2019-11-12"
## [46] NA NA "2019-10-01" "2000-08-17" "2001-04-11"
## [51] NA "2016-06-30" NA NA "2013-05-23"
## [56] "2017-02-24" NA "2004-11-02" "2019-03-06" "2018-09-01"
## [61] NA "2002-12-31" "2013-07-27" "2014-01-10" "2011-12-14"
## [66] NA "2008-03-01" "2018-05-07" "2017-11-23" NA
## [71] "2008-09-27" NA "2008-01-07" NA "2005-05-11"
## [76] "2003-08-12" NA NA NA "2014-11-25"
## [81] NA NA NA "2008-04-01" NA
## [86] "2002-10-01" "2011-03-25" "2000-07-11" "2014-10-19" NA
## [91] "2013-06-20" "2008-01-16" "2016-06-24" NA NA
## [96] "2007-04-29" NA NA</code></pre>
<p>For more details, go to the <em>Date Formats</em> section of <a href="https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/intermediate-r.html#utilities">Utilities</a> chapter of <em>Intermediate R</em>.</p>
<p>Convert the dates in the <code>date_opened</code> column to the same format using the <code>formats</code> vector and store this as a new column called <code>date_opened_clean</code>:</p>
<div class="sourceCode" id="cb503"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb503-1"><a href="cleaning-data-in-r.html#cb503-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Define the date formats</span></span>
<span id="cb503-2"><a href="cleaning-data-in-r.html#cb503-2" aria-hidden="true" tabindex="-1"></a>formats <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"%Y-%m-%d"</span>, <span class="st">"%B %d, %Y"</span>)</span>
<span id="cb503-3"><a href="cleaning-data-in-r.html#cb503-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb503-4"><a href="cleaning-data-in-r.html#cb503-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert dates to the same format</span></span>
<span id="cb503-5"><a href="cleaning-data-in-r.html#cb503-5" aria-hidden="true" tabindex="-1"></a>accounts[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,] <span class="sc">%>%</span></span>
<span id="cb503-6"><a href="cleaning-data-in-r.html#cb503-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">date_opened_clean =</span> <span class="fu">parse_date_time</span>(date_opened, formats))</span></code></pre></div>
<pre><code>## id date_opened total date_opened_clean
## 1 A880C79F 2003-10-19 169305 2003-10-19
## 2 BE8222DF October 05, 2018 107460 2018-10-05
## 3 19F9E113 2008-07-29 15297152 2008-07-29
## 4 A2FE52A3 2005-06-09 14897272 2005-06-09
## 5 F6DC2C08 2012-03-31 124568 2012-03-31
## 6 D2E55799 2007-06-20 13635752 2007-06-20
## 7 53AE87EF December 01, 2017 15375984 2017-12-01
## 8 3E97F253 2019-06-03 14515800 2019-06-03
## 9 4AE79EA1 2011-05-07 23338536 2011-05-07
## 10 2322DFB4 2018-04-07 189524 2018-04-07</code></pre>
<p><strong>Currency uniformity</strong></p>
<p>Now that dates are in order, correct any unit differences. First, plot the data, there’s a group of very high values, and a group of relatively lower values. The bank has two different offices - one in New York, and one in Tokyo, so the accounts managed by the Tokyo office are in Japanese yen instead of U.S.</p>
<p>Create a scatter plot with <code>date_opened</code> on the x-axis and <code>total</code> on the y-axis:</p>
<div class="sourceCode" id="cb505"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb505-1"><a href="cleaning-data-in-r.html#cb505-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Scatter plot of opening date and total amount</span></span>
<span id="cb505-2"><a href="cleaning-data-in-r.html#cb505-2" aria-hidden="true" tabindex="-1"></a>accounts <span class="sc">%>%</span></span>
<span id="cb505-3"><a href="cleaning-data-in-r.html#cb505-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> date_opened, <span class="at">y =</span> total)) <span class="sc">+</span></span>
<span id="cb505-4"><a href="cleaning-data-in-r.html#cb505-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>()</span></code></pre></div>
<p><img src="09-Cleaning_Data_in_R_files/figure-html/unnamed-chunk-29-1.png" width="672" /></p>
<p>Left join <code>accounts</code> and <code>account_offices</code> by their <code>id</code> columns.</p>
<p>Convert the <code>totals</code> from the Tokyo office from yen to dollars, and keep the <code>total</code> from the New York office in dollars. Store this as a new column called <code>total_usd</code>:</p>
<div class="sourceCode" id="cb506"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb506-1"><a href="cleaning-data-in-r.html#cb506-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Left join accounts to account_offices by id</span></span>
<span id="cb506-2"><a href="cleaning-data-in-r.html#cb506-2" aria-hidden="true" tabindex="-1"></a>accounts[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,] <span class="sc">%>%</span></span>
<span id="cb506-3"><a href="cleaning-data-in-r.html#cb506-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(account_offices, <span class="at">by =</span> <span class="st">"id"</span>) <span class="sc">%>%</span></span>
<span id="cb506-4"><a href="cleaning-data-in-r.html#cb506-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb506-5"><a href="cleaning-data-in-r.html#cb506-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># Convert totals from the Tokyo office to USD</span></span>
<span id="cb506-6"><a href="cleaning-data-in-r.html#cb506-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">total_usd =</span> <span class="fu">ifelse</span>(office <span class="sc">==</span> <span class="st">"Tokyo"</span>, total <span class="sc">/</span> <span class="dv">104</span>, total))</span></code></pre></div>
<pre><code>## id date_opened total office total_usd
## 1 A880C79F 2003-10-19 169305 New York 169305
## 2 BE8222DF October 05, 2018 107460 New York 107460
## 3 19F9E113 2008-07-29 15297152 Tokyo 147088
## 4 A2FE52A3 2005-06-09 14897272 Tokyo 143243
## 5 F6DC2C08 2012-03-31 124568 New York 124568
## 6 D2E55799 2007-06-20 13635752 Tokyo 131113
## 7 53AE87EF December 01, 2017 15375984 Tokyo 147846
## 8 3E97F253 2019-06-03 14515800 Tokyo 139575
## 9 4AE79EA1 2011-05-07 23338536 Tokyo 224409
## 10 2322DFB4 2018-04-07 189524 New York 189524</code></pre>
<p><strong>Cross field validation</strong></p>
<p>Cross field validation basically means cross-checking/comparing with other columns to make sure the compared column values make sense.</p>
<center>
<strong>Validating totals</strong>
</center>
<p>There are three different funds that account holders can store their money in. In this exercise, validate whether the total amount in each account is equal to the sum of the amount in <code>fund_A</code>, <code>fund_B</code>, and <code>fund_C</code>.</p>
<p>Create a new column called <code>theoretical_total</code> that contains the sum of the amounts in each fund.</p>
<p>Find the accounts where the <code>total</code> doesn’t match the <code>theoretical_total</code>.</p>
<div class="sourceCode" id="cb508"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb508-1"><a href="cleaning-data-in-r.html#cb508-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Find invalid totals</span></span>
<span id="cb508-2"><a href="cleaning-data-in-r.html#cb508-2" aria-hidden="true" tabindex="-1"></a>accounts_funds <span class="sc">%>%</span></span>
<span id="cb508-3"><a href="cleaning-data-in-r.html#cb508-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># theoretical_total: sum of the three funds</span></span>
<span id="cb508-4"><a href="cleaning-data-in-r.html#cb508-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">theoretical_total =</span> fund_A <span class="sc">+</span> fund_B <span class="sc">+</span> fund_C) <span class="sc">%>%</span></span>
<span id="cb508-5"><a href="cleaning-data-in-r.html#cb508-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># Find accounts where total doesn't match theoretical_total</span></span>
<span id="cb508-6"><a href="cleaning-data-in-r.html#cb508-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(theoretical_total <span class="sc">!=</span> total)</span></code></pre></div>
<pre><code>## id date_opened total fund_A fund_B fund_C acct_age theoretical_total
## 1 D5EB0F00 2001-04-16 130920 69487 48681 56408 19 174576
## 2 92C237C6 2005-12-13 85362 72556 21739 19537 15 113832
## 3 0E5B69F5 2018-05-07 134488 88475 44383 46475 2 179333</code></pre>
<p><strong>Validating age</strong></p>
<p>Now that some inconsistencies in the <code>total</code> amounts been found, there may also be inconsistencies in the <code>acct_age</code> column, maybe these inconsistencies are related. Validate the age of each account and see if rows with inconsistent <code>acct_age</code>s are the same ones that had inconsistent <code>total</code>s.</p>
<p>Create a new column called <code>theoretical_age</code> that contains the age of each account based on the <code>date_opened.</code></p>
<p>Find the accounts where the <code>acct_age</code> doesn’t match the <code>theoretical_age.</code></p>
<div class="sourceCode" id="cb510"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb510-1"><a href="cleaning-data-in-r.html#cb510-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Find invalid acct_age</span></span>
<span id="cb510-2"><a href="cleaning-data-in-r.html#cb510-2" aria-hidden="true" tabindex="-1"></a>accounts_funds <span class="sc">%>%</span></span>
<span id="cb510-3"><a href="cleaning-data-in-r.html#cb510-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># theoretical_age: age of acct based on date_opened</span></span>
<span id="cb510-4"><a href="cleaning-data-in-r.html#cb510-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">theoretical_age =</span> <span class="fu">floor</span>(<span class="fu">as.numeric</span>(date_opened <span class="sc">%--%</span> <span class="fu">today</span>(), <span class="st">"years"</span>))) <span class="sc">%>%</span></span>
<span id="cb510-5"><a href="cleaning-data-in-r.html#cb510-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># Filter for rows where acct_age is different from theoretical_age</span></span>
<span id="cb510-6"><a href="cleaning-data-in-r.html#cb510-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(acct_age <span class="sc">!=</span> theoretical_age)</span></code></pre></div>
<pre><code>## id date_opened total fund_A fund_B fund_C acct_age theoretical_age
## 1 A880C79F 2003-10-19 169305 85018 75580 8707 17 19
## 2 BE8222DF 2018-10-05 107460 64784 35194 7482 2 4
## 3 19F9E113 2008-07-29 147088 64029 15300 67759 12 14
## 4 A2FE52A3 2005-06-09 143243 63466 54053 25724 15 17
## 5 F6DC2C08 2012-03-31 124568 21156 47935 55477 8 10
## 6 D2E55799 2007-06-20 131113 79241 26800 25072 13 15
## 7 53AE87EF 2017-12-01 147846 38450 29185 80211 3 5
## 8 3E97F253 2019-06-03 139575 11045 65907 62623 1 3
## 9 4AE79EA1 2011-05-07 224409 68394 80418 75597 9 11
## 10 2322DFB4 2018-04-07 189524 66964 52238 70322 2 4
## 11 645335B2 2018-11-16 154001 68691 56400 28910 2 4
## 12 D5EB0F00 2001-04-16 130920 69487 48681 56408 19 21
## 13 1EB593F7 2005-04-21 191989 75388 84199 32402 15 17
## 14 DDBA03D9 2006-06-13 92473 32931 22162 37380 14 16
## 15 40E4A2F4 2009-01-07 180547 82564 68210 29773 12 14
## 16 39132EEA 2012-07-07 150115 26358 74286 49471 8 10
## 17 387F8E4D 2011-01-03 90410 7520 67142 15748 10 12
## 18 11C3C3C0 2017-12-24 180003 84295 31591 64117 2 5
## 19 C2FC91E1 2004-05-21 105722 25398 24075 56249 16 18
## 20 FB8F01C1 2001-09-06 217068 69738 86768 60562 19 21
## 21 0128D2D0 2005-04-09 184421 82221 60149 42051 15 17
## 22 BE6E4B3F 2009-10-20 150769 49607 55417 45745 11 13
## 23 7C6E2ECC 2003-05-16 169814 82093 62756 24965 17 19
## 24 02E63545 2015-10-25 125117 50287 23342 51488 5 7
## 25 4399C98B 2001-05-19 130421 58177 43912 28332 19 21
## 26 98F4CF0F 2014-05-27 143211 84645 7088 51478 6 8
## 27 247222A6 2015-05-26 150372 69104 63369 17899 5 7
## 28 420985EE 2008-12-27 123125 59390 27890 35845 12 14
## 29 0E3903BA 2015-11-11 182668 47236 87437 47995 5 7
## 30 64EF994F 2009-02-26 161141 89269 25939 45933 11 13
## 31 CCF84EDB 2008-12-26 136128 33405 89016 13707 12 14
## 32 51C21705 2016-04-22 155684 53542 38234 63908 4 6
## 33 C868C6AD 2000-01-31 112818 17876 15057 79885 21 23
## 34 92C237C6 2005-12-13 85362 72556 21739 19537 15 17
## 35 9ECEADB2 2018-05-17 146153 40675 46482 58996 2 4
## 36 DF0AFE50 2004-12-03 146635 67373 63443 15819 16 18
## 37 5CD605B3 2016-10-19 87921 8474 50284 29163 4 6
## 38 402839E2 2019-09-14 163416 59213 23460 80743 1 3
## 39 78286CE7 2009-10-05 144704 72495 38450 33759 11 13
## 40 168E071B 2013-07-11 87826 21642 42937 23247 7 9
## 41 466CCDAA 2002-03-24 144051 19756 80182 44113 18 20
## 42 8DE1ECB9 2015-10-17 217975 67105 72907 77963 5 7
## 43 E19FE6B5 2009-06-06 101936 39942 38580 23414 11 13
## 44 1240D39C 2011-09-07 151556 18835 46135 86586 9 11
## 45 A7BFAA72 2019-11-12 133790 56001 54885 22904 1 3
## 46 C3D24436 2002-05-24 101584 58434 21069 22081 18 20
## 47 FAD92F0F 2007-09-13 164241 70211 73984 20046 13 15
## 48 236A1D51 2019-10-01 177759 20886 80883 75990 1 3
## 49 A6DDDC4C 2000-08-17 67962 5970 20088 41904 20 22
## 50 DDFD0B3D 2001-04-11 151696 30596 84390 36710 19 21
## 51 D13375E9 2005-11-01 134083 28545 37537 68001 15 17
## 52 AC50B796 2016-06-30 154916 54451 35906 64559 4 6
## 53 290319FD 2005-05-27 170178 54341 32764 83073 15 17
## 54 FC71925A 2006-11-02 186281 89127 43356 53798 14 16
## 55 7B0F3685 2013-05-23 179102 81321 18106 79675 7 9
## 56 BE411172 2017-02-24 170096 86735 56580 26781 3 5
## 57 58066E39 2015-09-16 163708 59004 16987 87717 5 7
## 58 EA7FF83A 2004-11-02 111526 86856 19406 5264 15 18
## 59 14A2DDB7 2019-03-06 123163 49666 25407 48090 1 3
## 60 305EEAA8 2018-09-01 138632 20307 35028 83297 2 4
## 61 8F25E54C 2008-11-24 189126 72037 62513 54576 12 14
## 62 19DD73C6 2002-12-31 141275 72872 51219 17184 18 20
## 63 ACB8E6AF 2013-07-27 71359 10203 51163 9993 7 9
## 64 91BFCC40 2014-01-10 132859 67405 7399 58055 7 9
## 65 86ACAF81 2011-12-14 235901 79599 79291 77011 9 11
## 66 77E85C14 2009-11-20 133348 20954 33018 79376 11 13
## 67 C5C6B79D 2008-03-01 188424 61972 69266 57186 12 14
## 68 0E5B69F5 2018-05-07 134488 88475 44383 46475 2 4
## 69 5275B518 2017-11-23 71665 16114 35691 19860 3 5
## 70 17217048 2001-05-25 193377 45365 58558 89454 19 21
## 71 E7496A7F 2008-09-27 142669 8615 72841 61213 12 14
## 72 41BBB7B4 2005-02-22 144229 26449 83938 33842 15 18
## 73 F6C7ABA1 2008-01-07 183440 82468 73281 27691 13 15
## 74 E699DF01 2008-02-17 199603 84788 47808 67007 12 15
## 75 BACA7378 2005-05-11 204271 87254 57043 59974 15 17
## 76 84A4302F 2003-08-12 186737 86632 33506 66599 17 19
## 77 F8A78C27 2006-04-05 41164 7560 21040 12564 14 16
## 78 8BADDF6A 2010-12-31 158203 25477 43902 88824 10 12
## 79 9FB57E68 2017-09-01 216352 86665 77117 52570 3 5
## 80 5C98E8F5 2014-11-25 103200 28990 24986 49224 6 8
## 81 6BB53C2A 2016-12-03 146394 29561 29023 87810 4 6
## 82 E23F2505 2017-10-15 121614 59013 39086 23515 3 5
## 83 0C121914 2017-06-21 227729 86625 79950 61154 3 5
## 84 3627E08A 2008-04-01 238104 60475 89011 88618 11 14
## 85 A94493B3 2009-08-01 85975 48482 7054 30439 11 13
## 86 0682E9DE 2002-10-01 72832 15809 15617 41406 18 20
## 87 49931170 2011-03-25 139614 83035 22239 34340 9 11
## 88 A154F63B 2000-07-11 133800 42648 16464 74688 20 22
## 89 3690CCED 2014-10-19 226595 70260 84337 71998 6 8
## 90 48F5E6D8 2020-02-16 135435 29123 23204 83108 0 3
## 91 515FAD84 2013-06-20 98190 6452 60014 31724 7 9
## 92 59794264 2008-01-16 157964 68869 32999 56096 13 15
## 93 2038185B 2016-06-24 194662 20591 89990 84081 4 6
## 94 65EAC615 2004-02-20 140191 20108 46764 73319 16 19
## 95 6C7509C9 2000-09-16 212089 58861 76975 76253 20 22
## 96 BD969A9D 2007-04-29 167238 10234 83183 73821 13 15
## 97 B0CDCE3D 2014-05-28 145240 62549 48606 34085 6 8
## 98 33A7F03E 2007-10-14 191839 80542 87909 23388 13 15</code></pre>
<p><strong>Visualizing missing data</strong></p>
<p>Dealing with missing data is one of the most common tasks in data science. There are a variety of types of missingness, as well as a variety of types of solutions to missing data.</p>
<p>A new version of the accounts data frame containing data on the <code>amount held</code> and <code>amount invested</code> for new and existing customers. However, there are rows with missing <code>inv_amount</code> values.</p>
<p>Visualize the missing values in <code>accounts</code> by column using <code>vis_miss()</code> from the <code>visdat</code> package.</p>
<div class="sourceCode" id="cb512"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb512-1"><a href="cleaning-data-in-r.html#cb512-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Visualize the missing values by column</span></span>
<span id="cb512-2"><a href="cleaning-data-in-r.html#cb512-2" aria-hidden="true" tabindex="-1"></a><span class="fu">vis_miss</span>(accounts_inv)</span></code></pre></div>
<pre><code>## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## Please use `gather()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.</code></pre>
<p><img src="09-Cleaning_Data_in_R_files/figure-html/unnamed-chunk-35-1.png" width="672" /></p>
<p>Most customers below 25 do not have investment accounts yet, and suspect it could be driving the missingness.</p>
<div class="sourceCode" id="cb514"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb514-1"><a href="cleaning-data-in-r.html#cb514-1" aria-hidden="true" tabindex="-1"></a>accounts_inv <span class="sc">%>%</span></span>
<span id="cb514-2"><a href="cleaning-data-in-r.html#cb514-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># missing_inv: Is inv_amount missing?</span></span>
<span id="cb514-3"><a href="cleaning-data-in-r.html#cb514-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">missing_inv =</span> <span class="fu">is.na</span>(inv_amount)) <span class="sc">%>%</span></span>
<span id="cb514-4"><a href="cleaning-data-in-r.html#cb514-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Group by missing_inv</span></span>
<span id="cb514-5"><a href="cleaning-data-in-r.html#cb514-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(missing_inv) <span class="sc">%>%</span></span>
<span id="cb514-6"><a href="cleaning-data-in-r.html#cb514-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># Calculate mean age for each missing_inv group</span></span>
<span id="cb514-7"><a href="cleaning-data-in-r.html#cb514-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">avg_age =</span> <span class="fu">mean</span>(age))</span></code></pre></div>
<pre><code>## # A tibble: 2 × 2
## missing_inv avg_age
## <lgl> <dbl>
## 1 FALSE 43.6
## 2 TRUE 21.8</code></pre>
<p>Since the average age for <code>TRUE</code> <code>missing_inv</code> is <code>22</code> and the average age for <code>FALSE</code> <code>missing_inv</code> is <code>44</code>, it is likely that the <code>inv_amount</code> variable is missing mostly in young customers.</p>
<div class="sourceCode" id="cb516"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb516-1"><a href="cleaning-data-in-r.html#cb516-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Sort by age and visualize missing vals</span></span>
<span id="cb516-2"><a href="cleaning-data-in-r.html#cb516-2" aria-hidden="true" tabindex="-1"></a>accounts_inv <span class="sc">%>%</span></span>
<span id="cb516-3"><a href="cleaning-data-in-r.html#cb516-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(age) <span class="sc">%>%</span></span>
<span id="cb516-4"><a href="cleaning-data-in-r.html#cb516-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">vis_miss</span>()</span></code></pre></div>
<p><img src="09-Cleaning_Data_in_R_files/figure-html/unnamed-chunk-37-1.png" width="672" /></p>
</div>
<div id="record-linkage" class="section level2 hasAnchor" number="9.4">
<h2><span class="header-section-number">9.4</span> Record Linkage<a href="cleaning-data-in-r.html#record-linkage" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><code>Damerau-Levenshtein</code> <code>distance</code> is used to identify how similar two strings are. As a reminder, <code>Damerau-Levenshtein</code> <code>distance</code> is the minimum number of steps needed to get from String A to String B, using these operations:</p>
<p><em>Insertion</em> of a new character.</p>
<p><em>Deletion</em> of an existing character.</p>
<p><em>Substitution</em> of an existing character.</p>
<p><em>Transposition</em> of two existing consecutive characters.</p>
<p>Use the <code>stringdist</code> package to compute string distances using various methods.</p>
<div class="sourceCode" id="cb517"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb517-1"><a href="cleaning-data-in-r.html#cb517-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate Damerau-Levenshtein distance</span></span>
<span id="cb517-2"><a href="cleaning-data-in-r.html#cb517-2" aria-hidden="true" tabindex="-1"></a><span class="fu">stringdist</span>(<span class="st">"las angelos"</span>, <span class="st">"los angeles"</span>, <span class="at">method =</span> <span class="st">"dl"</span>)</span></code></pre></div>
<pre><code>## [1] 2</code></pre>
<p>LCS (Longest Common Subsequence) only considers <em>Insertion</em> and <em>Deletion</em>.</p>
<div class="sourceCode" id="cb519"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb519-1"><a href="cleaning-data-in-r.html#cb519-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate LCS distance</span></span>
<span id="cb519-2"><a href="cleaning-data-in-r.html#cb519-2" aria-hidden="true" tabindex="-1"></a><span class="fu">stringdist</span>(<span class="st">"las angelos"</span>, <span class="st">"los angeles"</span>, <span class="at">method =</span> <span class="st">"lcs"</span>)</span></code></pre></div>
<pre><code>## [1] 4</code></pre>
<div class="sourceCode" id="cb521"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb521-1"><a href="cleaning-data-in-r.html#cb521-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate Jaccard distance</span></span>
<span id="cb521-2"><a href="cleaning-data-in-r.html#cb521-2" aria-hidden="true" tabindex="-1"></a><span class="fu">stringdist</span>(<span class="st">"las angelos"</span>, <span class="st">"los angeles"</span>, <span class="at">method =</span> <span class="st">"jaccard"</span>)</span></code></pre></div>
<pre><code>## [1] 0</code></pre>
<p><strong>Fixing typos with string distance</strong></p>
<p><code>zagat</code>, is a set of restaurants in New York, Los Angeles, Atlanta, San Francisco, and Las Vegas. The data is from Zagat, a company that collects restaurant reviews, and includes the restaurant names, addresses, phone numbers, as well as other restaurant information.</p>
<p>The <code>city</code> column contains the name of the city that the restaurant is located in. However, there are a number of typos throughout the column. Map each <code>city</code> to one of the five correctly-spelled cities contained in the <code>cities</code> data frame.</p>
<p>Left join <code>zagat</code> and <code>cities</code> based on string distance using the <code>city</code> and <code>city_actual</code> columns.</p>
<p><code>stringdist_left_join</code> function from the <code>fuzzyjoin</code> package that allows you to do a <code>stringdist</code> left join.</p>
<div class="sourceCode" id="cb523"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb523-1"><a href="cleaning-data-in-r.html#cb523-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Count the number of each city variation</span></span>
<span id="cb523-2"><a href="cleaning-data-in-r.html#cb523-2" aria-hidden="true" tabindex="-1"></a>zagat[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,] <span class="sc">%>%</span></span>
<span id="cb523-3"><a href="cleaning-data-in-r.html#cb523-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(city)</span></code></pre></div>
<pre><code>## city n
## 1 llos angeles 1
## 2 lo angeles 2
## 3 los anegeles 1
## 4 los angeles 6</code></pre>
<div class="sourceCode" id="cb525"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb525-1"><a href="cleaning-data-in-r.html#cb525-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Join and look at results</span></span>
<span id="cb525-2"><a href="cleaning-data-in-r.html#cb525-2" aria-hidden="true" tabindex="-1"></a>zagat[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,] <span class="sc">%>%</span></span>
<span id="cb525-3"><a href="cleaning-data-in-r.html#cb525-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># Left join based on stringdist using city and city_actual cols</span></span>
<span id="cb525-4"><a href="cleaning-data-in-r.html#cb525-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">stringdist_left_join</span>(cities, <span class="at">by =</span> <span class="fu">c</span>(<span class="st">"city"</span> <span class="ot">=</span> <span class="st">"city_actual"</span>)) <span class="sc">%>%</span></span>