-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-Cleaning_Data_in_R.Rmd
1968 lines (1756 loc) · 114 KB
/
09-Cleaning_Data_in_R.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Cleaning Data in R
<https://learn.datacamp.com/courses/cleaning-data-in-r>
```{r include=FALSE}
library(ggplot2)
library(dplyr)
library(tidyr)
library(readr)
library(forcats)
library(stringr)
library(scales)
library(lubridate)
library(assertive)
library(visdat)
library(stringdist)
library(fuzzyjoin)
library(reclin)
bike_share_rides <- readRDS(gzcon(url("https://assets.datacamp.com/production/repositories/5698/datasets/5d0ed31a0b5c3a63a75cfca7d12c7f7fec1c7521/bike_share_rides_ch1_1.rds")))
sfo_survey <- readRDS(gzcon(url("https://assets.datacamp.com/production/repositories/5698/datasets/d3e478e1482be254e824a801e18996ca482a6878/sfo_survey_ch2_1.rds")))
accounts <- readRDS(gzcon(url("https://assets.datacamp.com/production/repositories/5698/datasets/d8129bcda468694f0aa0ae7d63328c970fb86788/ch3_1_accounts.rds")))
```
## Common Data Problems
**Converting data types**
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 `glimpse()`:
```{r}
# Glimpse at bike_share_rides
glimpse(bike_share_rides)
# Summary of user_birth_year
summary(bike_share_rides$user_birth_year)
```
The summary statistics of `user_birth_year` don't seem to offer much useful information about the different birth years because the `user_birth_year` column is a `numeric` type and should be converted to a `factor`.
Use `dplyr` and `assertive` packages to convert a column into a factor and assert/confirm whether a column is the type wanted or not.
Use `as.___()` functions to convert objects to a new data type.
Use `assert_is____()` functions to confirm an object's data type.
```{r}
# Convert user_birth_year to factor: user_birth_year_fct
bike_share_rides <- bike_share_rides %>%
mutate(user_birth_year_fct = as.factor(user_birth_year))
```
If the `assert` is `TRUE`, nothing will be outputted:
```{r}
# Assert user_birth_year_fct is a factor
assert_is_factor(bike_share_rides$user_birth_year_fct)
```
```{r}
# Summary of user_birth_year_fct
summary(bike_share_rides$user_birth_year_fct)
```
**Trimming strings**
Another common dirty data problem is having extra bits like percent signs or periods in numbers, causing them to be read in as `character`.
Use `str_remove()` to remove `"minutes"` from the `duration` column of `bike_share_rides.` Add this as a new column called `duration_trimmed`.
Convert the `duration_trimmed` column to a numeric type and add this as a new column called `duration_mins`.
`Glimpse` at `bike_share_rides` and `assert` that the `duration_mins` column is `numeric`.
```{r}
bike_share_rides <- bike_share_rides %>%
# Remove 'minutes' from duration: duration_trimmed
mutate(duration_trimmed = str_remove(duration, "minutes"),
# Convert duration_trimmed to numeric: duration_mins
duration_mins = as.numeric(duration_trimmed))
```
```{r}
# Glimpse at bike_share_rides
glimpse(bike_share_rides)
```
```{r}
# Assert duration_mins is numeric
assert_is_numeric(bike_share_rides$duration_mins)
```
For more details, go to the *String Wrangling* section at the bottom of [Transform your data](https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/working-with-data-in-the-tidyverse.html#transform-your-data) chapter of *Working with Data in the Tidyverse*.
**Range constraints**
<center>**Time range**</center>
Values that are out of range can throw off an analysis, so it's important to catch them early on.
examine the `duration_min` 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.
Create a three-bin histogram of the `duration_min` column of `bike_share_rides` using `ggplot2` to identify if there is out-of-range data.
Replace the values of `duration_min` that are greater than `1440` minutes (24 hours) with `1440.` Add this to `bike_share_rides` as a new column called `duration_min_const`.
Assert that all values of `duration_min_const` are between `0` and `1440`:
```{r}
# Create breaks
breaks <- c(min(bike_share_rides$duration_mins), 0, 1440, max(bike_share_rides$duration_mins))
# Create a histogram of duration_min
ggplot(bike_share_rides, aes(duration_mins)) +
geom_histogram(breaks = breaks)
# duration_min_const: replace vals of duration_min > 1440 with 1440
bike_share_rides <- bike_share_rides %>%
mutate(duration_min_const = replace(duration_mins, duration_mins > 1440, 1440))
# Make sure all values of duration_min_const are between 0 and 1440
assert_all_are_in_closed_range(bike_share_rides$duration_min_const, lower = 0, upper = 1440)
```
<center>**Date range**</center>
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.
Convert the `date` column of `bike_share_rides` from `character` to the `Date` data type.
`Assert` that all values in the `date` column happened sometime in the past and not in the future.
```{r}
# Convert date to Date type
bike_share_rides <- bike_share_rides %>%
mutate(date = as.Date(date))
# Make sure all dates are in the past
assert_all_are_in_past(bike_share_rides$date)
```
Filter `bike_share_rides` to get only the rides from the past or today, and save this as `bike_share_rides_past.`
`Assert` that the dates in `bike_share_rides_past` occurred only in the past.
```{r}
# Filter for rides that occurred before or on today's date
bike_share_rides_past <- bike_share_rides %>%
filter(date <= today())
# Make sure all dates from bike_share_rides_past are in the past
assert_all_are_in_past(bike_share_rides_past$date)
```
**Uniqueness constraints**
<center>**Full duplicates**</center>
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 `mean` and `median.`
Get the total number of full duplicates in `bike_share_rides`.
Remove all full duplicates from `bike_share_rides` and save the new data frame as `bike_share_rides_unique`.
Get the total number of full duplicates in the new `bike_share_rides_unique` data frame.
```{r}
# Count the number of full duplicates
sum(duplicated(bike_share_rides))
# Remove duplicates
bike_share_rides_unique <- distinct(bike_share_rides)
# Count the full duplicates in bike_share_rides_unique
sum(duplicated(bike_share_rides_unique))
```
<center>**Partial duplicates**</center>
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.
Remove full and partial duplicates from `bike_share_rides` based on `ride_id` only, keeping all columns. Store this as `bike_share_rides_unique`.
```{r}
# Remove full and partial duplicates
bike_share_rides_unique <- bike_share_rides %>%
# Only based on ride_id instead of all cols
distinct(ride_id, .keep_all = TRUE)
# Find duplicated ride_ids in bike_share_rides_unique
bike_share_rides_unique %>%
# Count the number of occurrences of each ride_id
count(ride_id) %>%
# Filter for rows with a count > 1
filter(n > 1)
```
**Aggregating partial duplicates**
Another way of handling partial duplicates is to compute a summary statistic of the values that differ between partial duplicates, such as `mean`, `median`, `maximum`, or `minimum.` 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).
```{r}
bike_share_rides %>%
# Group by ride_id and date
group_by(ride_id, date) %>%
# Add duration_min_avg column
mutate(duration_min_avg = mean(duration_mins)) %>%
# Remove duplicates based on ride_id and date, keep all cols
distinct(ride_id, date, .keep_all = TRUE) %>%
# Remove duration_min column
select(-duration_mins)
```
## Categorical and Text Data
**Membership data range**
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.
`Count` the number of occurrences of each `dest_size` in `sfo_survey`.
`"huge"`, `" Small "`, `"Large "`, and `" Hub"` appear to violate membership constraints.
```{r}
# Count the number of occurrences of dest_size
sfo_survey %>%
count(dest_size)
```
Use the correct filtering join on `sfo_survey` and `dest_sizes` to get the rows of `sfo_survey` that have a valid `dest_size`:
```{r}
dest_sizes <- structure(list(dest_size = c("Small", "Medium", "Large", "Hub"
), passengers_per_day = structure(c(1L, 3L, 4L, 2L), .Label = c("0-20K",
"100K+", "20K-70K", "70K-100K"), class = "factor")), .Names = c("dest_size",
"passengers_per_day"), row.names = c(NA, -4L), class = "data.frame")
```
```{r}
# Remove bad dest_size rows
sfo_survey %>%
# Join with dest_sizes
semi_join(dest_sizes, by = "dest_size")%>%
# Count the number of each dest_size
count(dest_size)
```
**Identifying inconsistency**
Sometimes, there are different kinds of inconsistencies that can occur within categories, making it look like a variable has more categories than it should.
Examine the `dest_size` column again as well as the `cleanliness` column and determine what kind of issues, if any, these two categorical variables face.
Count the number of occurrences of each category of the `dest_size` variable of `sfo_survey`. The categories in `dest_size` have **inconsistent white space**:
```{r}
# Count dest_size
sfo_survey %>%
count(dest_size)
```
Count the number of occurrences of each category of the `cleanliness` variable of `sfo_survey`. The categories in `cleanliness` have **inconsistent capitalization**.
```{r}
# Count cleanliness
sfo_survey %>%
count(cleanliness)
```
**Correcting inconsistency**
`dest_size` has whitespace inconsistencies and cleanliness has capitalization inconsistencies, use the new tools to fix the inconsistent values in `sfo_survey` instead of removing the data points entirely.
Add a column to `sfo_survey` called `dest_size_trimmed` that contains the values in the `dest_size` column with all leading and trailing whitespace removed.
Add another column called `cleanliness_lower` that contains the values in the `cleanliness` column converted to all lowercase.
```{r}
# Add new columns to sfo_survey
sfo_survey <- sfo_survey %>%
# dest_size_trimmed: dest_size without whitespace
mutate(dest_size_trimmed = str_trim(dest_size),
# cleanliness_lower: cleanliness converted to lowercase
cleanliness_lower = str_to_lower(cleanliness))
# Count values of dest_size_trimmed
sfo_survey %>%
count(dest_size_trimmed)
# Count values of cleanliness_lower
sfo_survey %>%
count(cleanliness_lower)
```
**Collapsing categories**
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:
```{r}
# Count categories of dest_region
sfo_survey %>%
count(dest_region)
```
`"EU"`, `"eur"`, and `"Europ"` need to be collapsed to `"Europe"`.
Create a vector called `europe_categories` containing the three values of `dest_region` that need to be collapsed.
Add a new column to `sfo_survey` called `dest_region_collapsed` that contains the values from the `dest_region` column, except the categories stored in `europe_categories` should be collapsed to Europe.
```{r}
# Count categories of dest_region
sfo_survey %>%
count(dest_region)
# Categories to map to Europe
europe_categories <- c("Europ", "eur", "EU")
# Add a new col dest_region_collapsed
sfo_survey %>%
# Map all categories in europe_categories to Europe
mutate(dest_region_collapsed = fct_collapse(dest_region,
Europe = europe_categories)) %>%
# Count categories of dest_region_collapsed
count(dest_region_collapsed)
```
For more details, go to the *(How To Collapse/Merge Levels)* section of [Manipulating Factor Variables](https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/categorical-data-in-the-tidyverse.html#manipulating-factor-variables) chapter of *Categorical Data in the Tidyverse*.
**Detecting inconsistent text data**
Sometimes, in a column, there are inconsistent observations in different formats.
Filter for rows with phone numbers that contain `"("`, or `")"`. Remember to use `fixed()` when searching for parentheses.
```{r}
sfo_survey[1:10,] %>%
filter(str_detect(safety, "safe") | str_detect(safety, "danger"))
```
For more details, go to the *String Wrangling* section at the bottom of [Transform your data](https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/working-with-data-in-the-tidyverse.html#transform-your-data) chapter of *Working with Data in the Tidyverse*.
**Replacing and removing**
The `str_remove_all()` function will remove all instances of the string passed to it.
```{r}
sfo_survey[1:10,] %>%
mutate(safe_or_not = str_remove_all(safety, "Somewhat")) %>%
select(airline, safe_or_not)
```
Again, go to the *String Wrangling* section at the bottom of [Transform your data](https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/working-with-data-in-the-tidyverse.html#transform-your-data)
**Filter/select observations with certain length**
The `str_length()` function takes in a character vector, returns a number for each element that indicates the length of each element.
```{r}
clean_only <- sfo_survey %>%
filter(str_length(cleanliness_lower) == 5)
clean_only[1:10,] %>%
select(airline, cleanliness_lower)
```
## Advanced Data Problems
**Date uniformity**
Make sure that the `accounts` dataset doesn't contain any uniformity problems. In this exercise, investigate the `date_opened` column and clean it up so that all the dates are in the same format.
By default, `as.Date()` can't convert `"Month DD, YYYY"` formats:
```{r}
as.Date(accounts$date_opened)
```
For more details, go to the *Date Formats* section of [Utilities](https://econ380w21.github.io/bpAlNw1Ae7YwY9H3f/intermediate-r.html#utilities) chapter of *Intermediate R*.
Convert the dates in the `date_opened` column to the same format using the `formats` vector and store this as a new column called `date_opened_clean`:
```{r}
# Define the date formats
formats <- c("%Y-%m-%d", "%B %d, %Y")
# Convert dates to the same format
accounts[1:10,] %>%
mutate(date_opened_clean = parse_date_time(date_opened, formats))
```
**Currency uniformity**
```{r include=FALSE}
account_offices <- structure(list(id = structure(c(67L, 76L, 13L, 64L, 96L, 84L,
39L, 26L, 35L, 16L, 44L, 85L, 14L, 86L, 28L, 25L, 24L, 7L, 77L,
99L, 1L, 75L, 52L, 2L, 31L, 60L, 18L, 30L, 5L, 45L, 82L, 37L,
81L, 59L, 61L, 88L, 43L, 27L, 50L, 10L, 32L, 56L, 89L, 8L, 66L,
78L, 98L, 17L, 65L, 87L, 83L, 69L, 19L, 100L, 51L, 74L, 40L,
94L, 9L, 20L, 57L, 12L, 70L, 58L, 54L, 49L, 80L, 6L, 38L, 11L,
93L, 29L, 95L, 92L, 72L, 53L, 97L, 55L, 62L, 42L, 47L, 91L, 4L,
22L, 68L, 3L, 34L, 63L, 23L, 33L, 36L, 41L, 15L, 46L, 48L, 73L,
71L, 21L), .Label = c("0128D2D0", "02E63545", "0682E9DE", "0C121914",
"0E3903BA", "0E5B69F5", "11C3C3C0", "1240D39C", "14A2DDB7", "168E071B",
"17217048", "19DD73C6", "19F9E113", "1EB593F7", "2038185B", "2322DFB4",
"236A1D51", "247222A6", "290319FD", "305EEAA8", "33A7F03E", "3627E08A",
"3690CCED", "387F8E4D", "39132EEA", "3E97F253", "402839E2", "40E4A2F4",
"41BBB7B4", "420985EE", "4399C98B", "466CCDAA", "48F5E6D8", "49931170",
"4AE79EA1", "515FAD84", "51C21705", "5275B518", "53AE87EF", "58066E39",
"59794264", "5C98E8F5", "5CD605B3", "645335B2", "64EF994F", "65EAC615",
"6BB53C2A", "6C7509C9", "77E85C14", "78286CE7", "7B0F3685", "7C6E2ECC",
"84A4302F", "86ACAF81", "8BADDF6A", "8DE1ECB9", "8F25E54C", "91BFCC40",
"92C237C6", "98F4CF0F", "9ECEADB2", "9FB57E68", "A154F63B", "A2FE52A3",
"A6DDDC4C", "A7BFAA72", "A880C79F", "A94493B3", "AC50B796", "ACB8E6AF",
"B0CDCE3D", "BACA7378", "BD969A9D", "BE411172", "BE6E4B3F", "BE8222DF",
"C2FC91E1", "C3D24436", "C470A574", "C5C6B79D", "C868C6AD", "CCF84EDB",
"D13375E9", "D2E55799", "D5EB0F00", "DDBA03D9", "DDFD0B3D", "DF0AFE50",
"E19FE6B5", "E22CE6AF", "E23F2505", "E699DF01", "E7496A7F", "EA7FF83A",
"F6C7ABA1", "F6DC2C08", "F8A78C27", "FAD92F0F", "FB8F01C1", "FC71925A"
), class = "factor"), office = c("New York", "New York", "Tokyo",
"Tokyo", "New York", "Tokyo", "Tokyo", "Tokyo", "Tokyo", "New York",
"New York", "New York", "New York", "Tokyo", "New York", "Tokyo",
"Tokyo", "New York", "New York", "Tokyo", "Tokyo", "Tokyo", "New York",
"New York", "New York", "Tokyo", "New York", "New York", "New York",
"New York", "New York", "Tokyo", "Tokyo", "Tokyo", "New York",
"Tokyo", "New York", "New York", "Tokyo", "New York", "Tokyo",
"New York", "New York", "Tokyo", "New York", "New York", "Tokyo",
"Tokyo", "New York", "Tokyo", "Tokyo", "Tokyo", "New York", "New York",
"New York", "Tokyo", "Tokyo", "Tokyo", "Tokyo", "Tokyo", "New York",
"Tokyo", "New York", "New York", "Tokyo", "Tokyo", "New York",
"Tokyo", "New York", "Tokyo", "New York", "New York", "New York",
"New York", "New York", "Tokyo", "New York", "New York", "New York",
"New York", "New York", "New York", "New York", "New York", "New York",
"New York", "Tokyo", "New York", "New York", "New York", "New York",
"New York", "New York", "New York", "New York", "New York", "New York",
"New York")), row.names = c(NA, -98L), class = "data.frame", .Names = c("id",
"office"))
```
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.
Create a scatter plot with `date_opened` on the x-axis and `total` on the y-axis:
```{r}
# Scatter plot of opening date and total amount
accounts %>%
ggplot(aes(x = date_opened, y = total)) +
geom_point()
```
Left join `accounts` and `account_offices` by their `id` columns.
Convert the `totals` from the Tokyo office from yen to dollars, and keep the `total` from the New York office in dollars. Store this as a new column called `total_usd`:
```{r}
# Left join accounts to account_offices by id
accounts[1:10,] %>%
left_join(account_offices, by = "id") %>%
# Convert totals from the Tokyo office to USD
mutate(total_usd = ifelse(office == "Tokyo", total / 104, total))
```
**Cross field validation**
Cross field validation basically means cross-checking/comparing with other columns to make sure the compared column values make sense.
```{r include=FALSE}
accounts_funds <- structure(list(id = structure(c(67L, 76L, 13L, 64L, 96L, 84L,
39L, 26L, 35L, 16L, 44L, 85L, 14L, 86L, 28L, 25L, 24L, 7L, 77L,
99L, 1L, 75L, 52L, 2L, 31L, 60L, 18L, 30L, 5L, 45L, 82L, 37L,
81L, 59L, 61L, 88L, 43L, 27L, 50L, 10L, 32L, 56L, 89L, 8L, 66L,
78L, 98L, 17L, 65L, 87L, 83L, 69L, 19L, 100L, 51L, 74L, 40L,
94L, 9L, 20L, 57L, 12L, 70L, 58L, 54L, 49L, 80L, 6L, 38L, 11L,
93L, 29L, 95L, 92L, 72L, 53L, 97L, 55L, 62L, 42L, 47L, 91L, 4L,
22L, 68L, 3L, 34L, 63L, 23L, 33L, 36L, 41L, 15L, 46L, 48L, 73L,
71L, 21L), .Label = c("0128D2D0", "02E63545", "0682E9DE", "0C121914",
"0E3903BA", "0E5B69F5", "11C3C3C0", "1240D39C", "14A2DDB7", "168E071B",
"17217048", "19DD73C6", "19F9E113", "1EB593F7", "2038185B", "2322DFB4",
"236A1D51", "247222A6", "290319FD", "305EEAA8", "33A7F03E", "3627E08A",
"3690CCED", "387F8E4D", "39132EEA", "3E97F253", "402839E2", "40E4A2F4",
"41BBB7B4", "420985EE", "4399C98B", "466CCDAA", "48F5E6D8", "49931170",
"4AE79EA1", "515FAD84", "51C21705", "5275B518", "53AE87EF", "58066E39",
"59794264", "5C98E8F5", "5CD605B3", "645335B2", "64EF994F", "65EAC615",
"6BB53C2A", "6C7509C9", "77E85C14", "78286CE7", "7B0F3685", "7C6E2ECC",
"84A4302F", "86ACAF81", "8BADDF6A", "8DE1ECB9", "8F25E54C", "91BFCC40",
"92C237C6", "98F4CF0F", "9ECEADB2", "9FB57E68", "A154F63B", "A2FE52A3",
"A6DDDC4C", "A7BFAA72", "A880C79F", "A94493B3", "AC50B796", "ACB8E6AF",
"B0CDCE3D", "BACA7378", "BD969A9D", "BE411172", "BE6E4B3F", "BE8222DF",
"C2FC91E1", "C3D24436", "C470A574", "C5C6B79D", "C868C6AD", "CCF84EDB",
"D13375E9", "D2E55799", "D5EB0F00", "DDBA03D9", "DDFD0B3D", "DF0AFE50",
"E19FE6B5", "E22CE6AF", "E23F2505", "E699DF01", "E7496A7F", "EA7FF83A",
"F6C7ABA1", "F6DC2C08", "F8A78C27", "FAD92F0F", "FB8F01C1", "FC71925A"
), class = "factor"), date_opened = structure(c(1066521600, 1538697600,
1217289600, 1118275200, 1333152000, 1182297600, 1512086400, 1559520000,
1304726400, 1523059200, 1542326400, 987379200, 1114041600, 1150156800,
1231286400, 1341619200, 1294012800, 1514073600, 1085097600, 999734400,
1113004800, 1255996800, 1053043200, 1445731200, 990230400, 1401148800,
1432598400, 1230336000, 1447200000, 1235606400, 1230249600, 1461283200,
949276800, 1134432000, 1526515200, 1102032000, 1476835200, 1568419200,
1254700800, 1373500800, 1016928000, 1445040000, 1244246400, 1315353600,
1573516800, 1022198400, 1189641600, 1569888000, 966470400, 986947200,
1130803200, 1467244800, 1117152000, 1162425600, 1369267200, 1487894400,
1442361600, 1099353600, 1551830400, 1535760000, 1227484800, 1041292800,
1374883200, 1389312000, 1323820800, 1258675200, 1204329600, 1525651200,
1511395200, 990748800, 1222473600, 1109030400, 1199664000, 1203206400,
1115769600, 1060646400, 1144195200, 1293753600, 1504224000, 1416873600,
1480723200, 1508025600, 1498003200, 1207008000, 1249084800, 1033430400,
1301011200, 963273600, 1413676800, 1581811200, 1371686400, 1200441600,
1466726400, 1077235200, 969062400, 1177804800, 1401235200, 1192320000
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), total = c(169305,
107460, 147088, 143243, 124568, 131113, 147846, 139575, 224409,
189524, 154001, 130920, 191989, 92473, 180547, 150115, 90410,
180003, 105722, 217068, 184421, 150769, 169814, 125117, 130421,
143211, 150372, 123125, 182668, 161141, 136128, 155684, 112818,
85362, 146153, 146635, 87921, 163416, 144704, 87826, 144051,
217975, 101936, 151556, 133790, 101584, 164241, 177759, 67962,
151696, 134083, 154916, 170178, 186281, 179102, 170096, 163708,
111526, 123163, 138632, 189126, 141275, 71359, 132859, 235901,
133348, 188424, 134488, 71665, 193377, 142669, 144229, 183440,
199603, 204271, 186737, 41164, 158203, 216352, 103200, 146394,
121614, 227729, 238104, 85975, 72832, 139614, 133800, 226595,
135435, 98190, 157964, 194662, 140191, 212089, 167238, 145240,
191839), fund_A = c(85018L, 64784L, 64029L, 63466L, 21156L, 79241L,
38450L, 11045L, 68394L, 66964L, 68691L, 69487L, 75388L, 32931L,
82564L, 26358L, 7520L, 84295L, 25398L, 69738L, 82221L, 49607L,
82093L, 50287L, 58177L, 84645L, 69104L, 59390L, 47236L, 89269L,
33405L, 53542L, 17876L, 72556L, 40675L, 67373L, 8474L, 59213L,
72495L, 21642L, 19756L, 67105L, 39942L, 18835L, 56001L, 58434L,
70211L, 20886L, 5970L, 30596L, 28545L, 54451L, 54341L, 89127L,
81321L, 86735L, 59004L, 86856L, 49666L, 20307L, 72037L, 72872L,
10203L, 67405L, 79599L, 20954L, 61972L, 88475L, 16114L, 45365L,
8615L, 26449L, 82468L, 84788L, 87254L, 86632L, 7560L, 25477L,
86665L, 28990L, 29561L, 59013L, 86625L, 60475L, 48482L, 15809L,
83035L, 42648L, 70260L, 29123L, 6452L, 68869L, 20591L, 20108L,
58861L, 10234L, 62549L, 80542L), fund_B = c(75580L, 35194L, 15300L,
54053L, 47935L, 26800L, 29185L, 65907L, 80418L, 52238L, 56400L,
48681L, 84199L, 22162L, 68210L, 74286L, 67142L, 31591L, 24075L,
86768L, 60149L, 55417L, 62756L, 23342L, 43912L, 7088L, 63369L,
27890L, 87437L, 25939L, 89016L, 38234L, 15057L, 21739L, 46482L,
63443L, 50284L, 23460L, 38450L, 42937L, 80182L, 72907L, 38580L,
46135L, 54885L, 21069L, 73984L, 80883L, 20088L, 84390L, 37537L,
35906L, 32764L, 43356L, 18106L, 56580L, 16987L, 19406L, 25407L,
35028L, 62513L, 51219L, 51163L, 7399L, 79291L, 33018L, 69266L,
44383L, 35691L, 58558L, 72841L, 83938L, 73281L, 47808L, 57043L,
33506L, 21040L, 43902L, 77117L, 24986L, 29023L, 39086L, 79950L,
89011L, 7054L, 15617L, 22239L, 16464L, 84337L, 23204L, 60014L,
32999L, 89990L, 46764L, 76975L, 83183L, 48606L, 87909L), fund_C = c(8707L,
7482L, 67759L, 25724L, 55477L, 25072L, 80211L, 62623L, 75597L,
70322L, 28910L, 56408L, 32402L, 37380L, 29773L, 49471L, 15748L,
64117L, 56249L, 60562L, 42051L, 45745L, 24965L, 51488L, 28332L,
51478L, 17899L, 35845L, 47995L, 45933L, 13707L, 63908L, 79885L,
19537L, 58996L, 15819L, 29163L, 80743L, 33759L, 23247L, 44113L,
77963L, 23414L, 86586L, 22904L, 22081L, 20046L, 75990L, 41904L,
36710L, 68001L, 64559L, 83073L, 53798L, 79675L, 26781L, 87717L,
5264L, 48090L, 83297L, 54576L, 17184L, 9993L, 58055L, 77011L,
79376L, 57186L, 46475L, 19860L, 89454L, 61213L, 33842L, 27691L,
67007L, 59974L, 66599L, 12564L, 88824L, 52570L, 49224L, 87810L,
23515L, 61154L, 88618L, 30439L, 41406L, 34340L, 74688L, 71998L,
83108L, 31724L, 56096L, 84081L, 73319L, 76253L, 73821L, 34085L,
23388L), acct_age = c(17, 2, 12, 15, 8, 13, 3, 1, 9, 2, 2, 19,
15, 14, 12, 8, 10, 2, 16, 19, 15, 11, 17, 5, 19, 6, 5, 12, 5,
11, 12, 4, 21, 15, 2, 16, 4, 1, 11, 7, 18, 5, 11, 9, 1, 18, 13,
1, 20, 19, 15, 4, 15, 14, 7, 3, 5, 15, 1, 2, 12, 18, 7, 7, 9,
11, 12, 2, 3, 19, 12, 15, 13, 12, 15, 17, 14, 10, 3, 6, 4, 3,
3, 11, 11, 18, 9, 20, 6, 0, 7, 13, 4, 16, 20, 13, 6, 13)), row.names = c(NA,
-98L), .Names = c("id", "date_opened", "total", "fund_A", "fund_B",
"fund_C", "acct_age"), class = "data.frame")
```
<center>**Validating totals**</center>
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 `fund_A`, `fund_B`, and `fund_C`.
Create a new column called `theoretical_total` that contains the sum of the amounts in each fund.
Find the accounts where the `total` doesn't match the `theoretical_total`.
```{r}
# Find invalid totals
accounts_funds %>%
# theoretical_total: sum of the three funds
mutate(theoretical_total = fund_A + fund_B + fund_C) %>%
# Find accounts where total doesn't match theoretical_total
filter(theoretical_total != total)
```
**Validating age**
Now that some inconsistencies in the `total` amounts been found, there may also be inconsistencies in the `acct_age` column, maybe these inconsistencies are related. Validate the age of each account and see if rows with inconsistent `acct_age`s are the same ones that had inconsistent `total`s.
Create a new column called `theoretical_age` that contains the age of each account based on the `date_opened.`
Find the accounts where the `acct_age` doesn't match the `theoretical_age.`
```{r}
# Find invalid acct_age
accounts_funds %>%
# theoretical_age: age of acct based on date_opened
mutate(theoretical_age = floor(as.numeric(date_opened %--% today(), "years"))) %>%
# Filter for rows where acct_age is different from theoretical_age
filter(acct_age != theoretical_age)
```
**Visualizing missing data**
```{r include=FALSE}
accounts_inv <- structure(list(cust_id = structure(c(45L, 76L, 57L, 50L, 77L,
11L, 22L, 52L, 43L, 9L, 5L, 13L, 86L, NA, NA, 72L, 24L, 74L,
88L, 58L, 54L, 66L, 38L, 64L, NA, NA, 82L, 20L, 87L, 62L, 15L,
NA, 27L, 69L, 61L, 47L, 59L, 51L, 67L, 2L, 49L, 34L, 83L, 18L,
41L, 78L, 19L, 42L, 10L, 16L, 23L, 39L, 79L, 56L, 33L, 70L, 63L,
7L, 29L, 28L, 25L, 30L, 37L, NA, 44L, 89L, 68L, 31L, 36L, 8L,
81L, 65L, NA, 32L, 35L, 21L, 14L, 75L, NA, 80L, 53L, 55L, 4L,
40L, 46L, 71L, 12L, 85L, 17L, 84L, 3L, 6L, 73L, 26L, 60L, 48L,
NA), .Label = c("", "0109137B", "014E0511", "078C654F", "0A9BA907",
"0B44C3F8", "0F0884F6", "13770971", "166B05B0", "1903EB99", "25E68E1B",
"296A9395", "2AB6539A", "2C5901B4", "2EC1B555", "2F4F99C1", "33CA2B76",
"38B8CD9C", "3B240FEF", "3C5CBBD7", "3E51A395", "3FA9296D", "46351200",
"472341F2", "4A13E345", "4C7F8638", "5321D380", "56D310A8", "58F8CC80",
"5AEA5AB8", "5F6A2443", "625167AC", "6B094617", "72DD1471", "777A7F2C",
"7A2879AF", "7A4EED75", "7A73F334", "7D8EBAF6", "807465A4", "80C0DAB3",
"82E87321", "870A9281", "87FDF627", "8C35540A", "8D08495A", "904A19DD",
"93A17007", "93E78DA3", "93F2F951", "96525DA6", "984403B9", "987DC93E",
"9B550FD5", "A07D5C92", "A1815565", "A631984D", "A69FA1B8", "A731C34E",
"A81D31B3", "AC2AEAC4", "ACE5C956", "B25B3B8D", "B40E8497", "B5D367B5",
"B99CD662", "BD7CF5D7", "BFC13E88", "C55C54A8", "C580AE41", "C9FB0E86",
"CA507BA1", "CEC1CAE5", "D3287768", "D4C7E817", "D5536652", "DE0A0882",
"DEC6DBE4", "E2EFF324", "E52D4C7F", "EC10469C", "EC189A55", "EC7C25A8",
"EEBD980F", "F2158F66", "F389832C", "F7FC8F78", "FA01676F", "FBAD3C91"
), class = "factor"), age = c(54L, 36L, 49L, 56L, 21L, 47L, 53L,
29L, 58L, 53L, 44L, 59L, 48L, 34L, 22L, 50L, 35L, 20L, 21L, 41L,
42L, 28L, 35L, 33L, 30L, 50L, 53L, 45L, 26L, 39L, 34L, 43L, 58L,
45L, 57L, 20L, 46L, 33L, 29L, 44L, 22L, 27L, 30L, 55L, 27L, 46L,
25L, 50L, 37L, 53L, 56L, 52L, 29L, 32L, 21L, 47L, 57L, 56L, 42L,
21L, 45L, 56L, 33L, 49L, 56L, 35L, 58L, 57L, 54L, 26L, 28L, 39L,
53L, 28L, 30L, 46L, 40L, 56L, 41L, 36L, 51L, 45L, 21L, 48L, 59L,
46L, 48L, 41L, 23L, 59L, 27L, 32L, 32L, 23L, 24L, 36L, 57L),
acct_amount = c(44244.71, 86506.85, 77799.33, 93875.24, 99998.35,
109737.62, 79744.23, 17939.88, 63523.31, 38175.46, 90469.53,
53796.13, 95380.06, 83653.09, 86028.48, 12209.84, 83127.65,
89961.77, 66947.3, 75207.99, 32891.31, 92838.44, 120512,
99771.9, 71782.2, 95038.14, 83343.18, 59678.01, 88049.82,
90413.25, 55976.78, 92007.12, 59700.08, 79630.02, 88440.54,
31981.36, 95352.02, 82511.24, 82084.76, 31730.19, 41942.23,
100683.48, 86503.33, 28834.71, 73951.45, 32220.83, 97856.46,
97833.54, 24267.02, 82058.48, 97595.3, 109943.03, 67297.46,
82996.04, 89855.98, 96673.37, 99193.98, 84505.81, 87146.19,
88660.4, 84107.71, 100266.99, 98923.14, 63182.57, 95275.46,
99141.9, 59863.77, 98047.16, 83345.15, 92750.87, 73618.75,
44226.86, 99490.61, 95315.71, 52684.17, 21757.14, 250046.76,
26585.87, 64944.62, 61795.89, 35924.41, 99577.36, 87312.64,
28827.59, 89138.52, 88682.34, 34679.6, 84132.1, 75508.61,
57838.49, 70272.97, 33984.87, 92169.14, 21942.37, 74010.15,
40651.36, 27907.16), inv_amount = c(35500.5, 81921.86, 46412.27,
76563.35, NA, 93552.69, 70357.7, 14429.59, 51297.32, 15052.7,
70173.49, 12401.32, 58388.14, 44656.36, NA, 7516.33, 67961.74,
NA, NA, 31620.86, 11993.35, 49090.83, 93233, 86992.74, 35476.83,
66797.81, 7282.91, 35939.08, 84432.03, 21574.21, 51478.91,
22053.26, 8145.24, 25250.82, 63332.9, NA, 84066.66, 33929.23,
44340.56, 21959.28, NA, 87882.91, 49180.36, 27532.35, 61650.12,
3216.72, NA, 61481.86, 22963.63, 35760.69, 82251.59, 81490.13,
57252.76, 30898.16, NA, 68468.28, 83364.21, 47826.51, 25759.85,
NA, 4217.92, 89342.43, 20932.3, 62692.03, 55888.87, 13468.4,
24569.47, 76216.88, 45162.06, 27963.45, 48979.16, 36572.69,
32150.64, 66914.63, 20970.35, 10582.94, 90442.57, 20441.92,
31803.34, 49387.29, 14881.89, 60408.99, NA, 14585.75, 60798.23,
26166.11, 28459.96, 23714.06, NA, 50814.83, 65969.8, 31395,
77896.86, NA, NA, 9387.87, 10967.69), account_opened = structure(c(10L,
57L, 69L, 59L, 16L, 73L, 58L, 20L, 5L, 79L, 40L, 6L, 7L,
41L, 14L, 71L, 39L, 11L, 33L, 62L, 2L, 13L, 37L, 43L, 84L,
9L, 12L, 6L, 78L, 87L, 17L, 86L, 31L, 89L, 34L, 77L, 35L,
64L, 50L, 56L, 30L, 60L, 23L, 48L, 8L, 29L, 63L, 52L, 47L,
90L, 51L, 4L, 74L, 21L, 18L, 82L, 80L, 22L, 52L, 67L, 48L,
28L, 83L, 27L, 70L, 85L, 68L, 42L, 88L, 44L, 76L, 45L, 1L,
23L, 75L, 14L, 15L, 19L, 54L, 61L, 90L, 49L, 36L, 55L, 24L,
53L, 66L, 21L, 46L, 25L, 26L, 32L, 72L, 38L, 3L, 81L, 65L
), .Label = c("01-08-17", "02-05-18", "02-06-18", "02-07-17",
"02-09-18", "03-01-19", "03-02-18", "03-04-17", "03-04-18",
"03-05-18", "03-09-18", "04-02-19", "04-05-17", "04-06-17",
"05-02-18", "05-06-17", "05-12-17", "06-02-18", "06-05-18",
"07-10-17", "07-11-17", "08-03-18", "08-06-17", "08-08-18",
"08-12-18", "09-02-19", "09-05-18", "09-06-18", "09-08-18",
"09-10-17", "09-10-18", "10-04-18", "10-08-18", "12-03-18",
"13-11-17", "14-04-17", "14-05-18", "14-07-18", "14-12-18",
"15-06-18", "15-08-18", "15-12-18", "16-05-17", "16-08-17",
"16-09-17", "16-11-17", "17-03-17", "17-09-18", "17-11-17",
"18-07-17", "18-08-18", "18-10-18", "19-05-18", "20-03-18",
"20-04-17", "20-04-18", "21-01-18", "21-06-18", "21-08-17",
"22-01-18", "22-05-17", "23-02-19", "23-05-18", "23-07-18",
"23-10-17", "24-12-17", "25-02-18", "25-04-18", "26-01-18",
"26-02-19", "26-05-18", "26-11-17", "26-12-17", "27-04-18",
"27-10-18", "27-12-18", "28-01-19", "28-02-18", "28-02-19",
"28-04-18", "28-05-17", "28-09-18", "28-11-17", "29-01-19",
"29-05-18", "29-07-18", "29-12-18", "30-08-18", "30-10-18",
"30-12-18"), class = "factor"), last_transaction = structure(c(88L,
44L, 19L, 34L, 46L, 41L, 74L, 55L, 64L, 90L, 83L, 52L, 72L,
54L, 21L, 37L, 66L, 58L, 71L, 29L, 82L, 39L, 57L, 14L, 69L,
79L, 91L, 5L, 86L, 87L, 62L, 32L, 11L, 56L, 2L, 70L, 43L,
21L, 81L, 24L, 47L, 50L, 4L, 16L, 61L, 53L, 38L, 60L, 63L,
36L, 59L, 65L, 33L, 88L, 45L, 51L, 12L, 75L, 31L, 85L, 28L,
9L, 1L, 80L, 78L, 6L, 4L, 42L, 30L, 73L, 23L, 8L, 13L, 9L,
77L, 68L, 35L, 27L, 48L, 76L, 20L, 49L, 17L, 89L, 15L, 18L,
56L, 26L, 7L, 10L, 67L, 84L, 25L, 3L, 40L, 22L, 35L), .Label = c("01-05-19",
"01-08-19", "02-02-19", "02-04-18", "02-10-18", "02-11-19",
"03-03-19", "03-04-19", "03-07-19", "04-01-20", "04-02-19",
"04-07-19", "04-08-19", "05-01-20", "05-02-19", "05-02-20",
"05-08-18", "06-08-19", "06-10-19", "06-12-18", "07-08-18",
"08-03-19", "08-06-18", "08-07-19", "08-10-18", "08-11-18",
"08-12-18", "09-02-19", "09-09-19", "09-11-19", "10-01-19",
"10-02-20", "10-07-18", "10-07-19", "11-07-19", "11-08-18",
"11-09-19", "11-10-18", "12-03-19", "12-09-18", "12-11-18",
"12-11-19", "13-01-19", "14-01-19", "14-02-19", "15-01-19",
"15-04-18", "15-11-19", "16-01-20", "17-05-18", "17-09-18",
"17-11-18", "17-11-19", "18-01-19", "18-05-18", "19-02-19",
"19-07-18", "19-10-18", "20-02-20", "21-07-18", "21-09-19",
"21-10-19", "21-11-18", "22-02-19", "22-02-20", "22-04-18",
"22-05-19", "22-09-18", "22-11-19", "23-06-19", "23-07-19",
"23-09-18", "24-04-19", "24-08-18", "24-08-19", "24-10-19",
"25-05-19", "25-06-18", "25-09-18", "26-01-20", "26-02-20",
"27-06-19", "28-08-18", "28-09-19", "29-07-18", "30-04-18",
"30-04-19", "30-09-19", "31-07-18", "31-10-18", "31-12-18"
), class = "factor")), .Names = c("cust_id", "age", "acct_amount",
"inv_amount", "account_opened", "last_transaction"), class = "data.frame", row.names = c(NA,
-97L))
```
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.
A new version of the accounts data frame containing data on the `amount held` and `amount invested` for new and existing customers. However, there are rows with missing `inv_amount` values.
Visualize the missing values in `accounts` by column using `vis_miss()` from the `visdat` package.
```{r}
# Visualize the missing values by column
vis_miss(accounts_inv)
```
Most customers below 25 do not have investment accounts yet, and suspect it could be driving the missingness.
```{r}
accounts_inv %>%
# missing_inv: Is inv_amount missing?
mutate(missing_inv = is.na(inv_amount)) %>%
# Group by missing_inv
group_by(missing_inv) %>%
# Calculate mean age for each missing_inv group
summarize(avg_age = mean(age))
```
Since the average age for `TRUE` `missing_inv` is `22` and the average age for `FALSE` `missing_inv` is `44`, it is likely that the `inv_amount` variable is missing mostly in young customers.
```{r}
# Sort by age and visualize missing vals
accounts_inv %>%
arrange(age) %>%
vis_miss()
```
## Record Linkage
`Damerau-Levenshtein` `distance` is used to identify how similar two strings are. As a reminder, `Damerau-Levenshtein` `distance` is the minimum number of steps needed to get from String A to String B, using these operations:
*Insertion* of a new character.
*Deletion* of an existing character.
*Substitution* of an existing character.
*Transposition* of two existing consecutive characters.
Use the `stringdist` package to compute string distances using various methods.
```{r}
# Calculate Damerau-Levenshtein distance
stringdist("las angelos", "los angeles", method = "dl")
```
LCS (Longest Common Subsequence) only considers *Insertion* and *Deletion*.
```{r}
# Calculate LCS distance
stringdist("las angelos", "los angeles", method = "lcs")
```
```{r}
# Calculate Jaccard distance
stringdist("las angelos", "los angeles", method = "jaccard")
```
**Fixing typos with string distance**
```{r include=FALSE}
cities <- structure(list(city_actual = structure(c(4L, 3L, 1L, 5L, 2L), .Label = c("atlanta",
"las vegas", "los angeles", "new york", "san francisco"), class = "factor")), .Names = "city_actual", row.names = c(NA,
-5L), class = "data.frame")
zagat <- structure(list(id = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 11L,
12L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 22L, 23L, 24L, 25L, 26L,
27L, 30L, 31L, 32L, 33L, 35L, 37L, 38L, 40L, 43L, 44L, 45L, 46L,
47L, 49L, 50L, 51L, 52L, 54L, 55L, 56L, 57L, 59L, 60L, 61L, 62L,
64L, 66L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L,
79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L,
92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L,
104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L,
115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L,
126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L,
137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L,
148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L,
159L, 160L, 161L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L,
171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L,
182L, 183L, 184L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L,
194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L, 204L,
205L, 206L, 207L, 208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L,
216L, 217L, 218L, 219L, 220L, 222L, 223L, 224L, 225L, 226L, 227L,
229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L, 238L, 239L,
240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L, 248L, 249L, 250L,
251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L,
262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L, 270L, 271L, 272L,
273L, 274L, 275L, 276L, 277L, 278L, 279L, 280L, 281L, 282L, 283L,
284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L, 292L, 293L, 294L,
295L, 296L, 297L, 298L, 299L, 300L, 301L, 302L, 303L, 304L, 305L,
306L, 307L, 308L, 309L, 310L, 311L, 312L, 313L, 314L, 315L, 316L,
317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L,
328L, 329L, 330L), name = c("apple pan the", "asahi ramen", "baja fresh",
"belvedere the", "benita's frites", "bernard's", "bistro 45",
"brighton coffee shop", "bristol farms market cafe", "cafe'50s",
"cafe blanc", "cassell's", "diaghilev", "don antonio's", "duke's",
"falafel king", "feast from the east", "gumbo pot the", "indo cafe",
"jan's family restaurant", "jiraffe", "jody maroni's sausage kingdom",
"joe's", "john o ` groats", "johnny rockets ( la )", "killer shrimp",
"kokomo cafe", "koo koo roo", "la salsa ( la )", "langer's",
"local nochol", "mani's bakery & espresso bar", "michael's ( los angeles )",
"mishima", "mo better meatty meat", "mulberry st.", "ocean park cafe",
"original pantry bakery", "parkway grill", "pho hoa", "pink's famous chili dogs",
"r-23", "rae's", "rubin's red hots", "ruby's ( la )", "ruth's chris steak house ( los angeles )",
"shiro", "sushi nozawa", "sweet lady jane", "tommy's", "water grill",
"afghan kebab house", "arcadia", "benny's burritos", "cafe con leche",
"corner bistro", "cucina della fontana", "cucina di pesce", "darbar",
"ej's luncheonette", "edison cafe", "elias corner", "good enough to eat",
"gray's papaya", "il mulino", "jackson diner", "joe's shanghai",
"john's pizzeria", "kelley & ping", "kiev", "kuruma zushi", "la caridad",
"la grenouille", "lemongrass grill", "lombardi's", "marnie's noodle shop",
"menchanko-tei", "mitali east-west", "monsoon ( ny )", "moustache",
"nobu", "one if by land tibs", "oyster bar", "palm", "palm too",
"patsy's pizza", "peter luger steak house", "rose of india",
"sam's noodle shop", "sarabeth's", "sparks steak house", "stick to your ribs",
"sushisay", "sylvia's", "szechuan hunan cottage", "szechuan kitchen",
"teresa's", "thai house cafe", "thailand restaurant", "veselka",
"westside cottage", "windows on the world", "wollensky's grill",
"yama", "zarela", "andre's french restaurant", "buccaneer bay club",
"buzio's in the rio", "'em eril's new orleans fish house", "fiore rotisserie & grille",
"hugo's cellar", "madame ching's", "mayflower cuisinier", "michael's ( las vegas )",
"monte carlo", "moongate", "morton's of chicago ( las vegas )",
"nicky blair's", "piero's restaurant", "spago ( las vegas )",
"steakhouse the", "stefano's", "sterling brunch", "tre visi",
"' 103 west", "alon's at the terrace", "baker's cajun cafe",
"barbecue kitchen", "bistro the", "bobby & june's kountry kitchen",
"bradshaw's restaurant", "brookhaven cafe", "cafe sunflower",
"canoe", "carey's", "carey's corner", "chops", "chopstix", "deacon burton's soulfood restaurant",
"eats", "flying biscuit the", "frijoleros", "greenwood's", "harold's barbecue",
"havana sandwich shop", "indian delights", "java jive", "johnny rockets ( at )",
"kalo's coffee house", "la fonda latina", "lettuce souprise you ( at )",
"majestic", "morton's of chicago ( atlanta )", "my thai", "nava",
"nuevo laredo cantina", "original pancake house ( at )", "palm the ( atlanta )",
"rainbow restaurant", "riviera", "silver skillet the", "soto",
"thelma's kitchen", "tortillas", "van gogh's restaurant & bar",
"veggieland", "white house restaurant", "bill's place", "cafe flore",
"caffe greco", "campo santo", "cha cha cha's", "doidge's", "dottie's true blue cafe",
"dusit thai", "ebisu", "'em erald garden restaurant", "eric's chinese restaurant",
"hamburger mary's", "kelly's on trinity", "la cumbre", "la mediterranee",
"la taqueria", "mario's bohemian cigar store cafe", "marnee thai",
"mel's drive-in", "mo's burgers", "phnom penh cambodian restaurant",
"roosevelt tamale parlor", "sally's cafe & bakery", "san francisco bbq",
"slanted door", "swan oyster depot", "thep phanom", "ti couz",
"trio cafe", "tu lan", "vicolo pizzeria", "wa-ha-ka oaxaca mexican grill",
"arnie morton's of chicago", "art's deli", "bel-air hotel", "campanile",
"chinois on main", "citrus", "fenix at the argyle", "granita",
"grill the", "l ` orangerie", "le chardonnay ( los angeles )",
"locanda veneta", "matsuhisa", "palm the ( los angeles )", "patina",
"philippe the original", "pinot bistro", "rex il ristorante",
"spago ( los angeles )", "valentino", "yujean kang's", "'21 club",
"aquavit", "aureole", "cafe lalo", "cafe des artistes", "carmine's",
"carnegie deli", "chanterelle", "daniel", "dawat", "felidia",
"four seasons", "gotham bar & grill", "gramercy tavern", "island spice",
"jo jo", "la caravelle", "la cote basque", "le bernardin", "les celebrites",
"lespinasse ( new york city )", "lutece", "manhattan ocean club",
"march", "mesa grill", "mi cocina", "montrachet", "oceana", "park avenue cafe ( new york city )",
"petrossian", "picholine", "pisces", "rainbow room", "river cafe",
"san domenico", "second avenue deli", "seryna", "shun lee palace",
"sign of the dove", "smith & wollensky", "tavern on the green",
"uncle nick's", "union square cafe", "virgil's real bbq", "chin's",
"coyote cafe ( las vegas )", "le montrachet bistro", "palace court",
"second street grill", "steak house the", "'till erman the",
"abruzzi", "bacchanalia", "bone's restaurant", "brasserie le coze",
"buckhead diner", "ciboulette restaurant", "delectables", "georgia grille",
"hedgerose heights inn the", "heera of india", "indigo coastal grill",
"la grotta", "mary mac's tea room", "nikolai's roof", "pano's & paul 's",
"ritz-carlton cafe ( buckhead )", "ritz-carlton dining room ( buckhead )",
"ritz-carlton restaurant", "toulouse", "veni vidi vici", "alain rondelli",
"aqua", "boulevard", "cafe claude", "campton place", "chez michel",
"fleur de lys", "fringale", "hawthorne lane", "khan toke thai house",
"la folie", "lulu restaurant-bis-cafe", "masa's", "mifune", "plumpjack cafe",
"postrio", "ritz-carlton dining room ( san francisco )", "rose pistola",
"ritz-carlton cafe ( atlanta )"), addr = c("10801 w. pico blvd.",
"2027 sawtelle blvd.", "3345 kimber dr.", "9882 little santa monica blvd.",
"1433 third st. promenade", "515 s. olive st.", "45 s. mentor ave.",
"9600 brighton way", "1570 rosecrans ave. s.", "838 lincoln blvd.",
"9777 little santa monica blvd.", "3266 w. sixth st.", "1020 n. san vicente blvd.",
"1136 westwood blvd.", "8909 sunset blvd.", "1059 broxton ave.",
"1949 westwood blvd.", "6333 w. third st.", "10428 1/2 national blvd.",
"8424 beverly blvd.", "502 santa monica blvd", "2011 ocean front walk",
"1023 abbot kinney blvd.", "10516 w. pico blvd.", "7507 melrose ave.",
"4000 colfax ave.", "6333 w. third st.", "8393 w. beverly blvd.",
"22800 pch", "704 s. alvarado st.", "30869 thousand oaks blvd.",
"519 s. fairfax ave.", "1147 third st.", "8474 w. third st.",
"7261 melrose ave.", "17040 ventura blvd.", "3117 ocean park blvd.",
"875 s. figueroa st. downtown", "510 s. arroyo pkwy .", "642 broadway",
"709 n. la brea ave.", "923 e. third st.", "2901 pico blvd.",
"15322 ventura blvd.", "45 s. fair oaks ave.", "224 s. beverly dr.",
"1505 mission st. s.", "11288 ventura blvd.", "8360 melrose ave.",
"2575 beverly blvd.", "544 s. grand ave.", "764 ninth ave.",
"21 e. 62nd st.", "93 ave. a", "424 amsterdam ave.", "331 w. fourth st.",
"368 bleecker st.", "87 e. fourth st.", "44 w. 56th st.", "432 sixth ave.",
"228 w. 47th st.", "24-02 31st st.", "483 amsterdam ave.", "2090 broadway",
"86 w. third st.", "37-03 74th st.", "9 pell st.", "48 w. 65th st.",
"127 greene st.", "117 second ave.", "2nd fl .", "2199 broadway",
"3 e. 52nd st.", "61a seventh ave.", "32 spring st.", "466 hudson st.",
"39 w. 55th st.", "296 bleecker st.", "435 amsterdam ave.", "405 atlantic ave.",
"105 hudson st.", "17 barrow st.", "` lower level", "837 second ave.",
"840 second ave.", "19 old fulton st.", "178 broadway", "308 e. sixth st.",
"411 third ave.", "1295 madison ave.", "210 e. 46th st.", "5-16 51st ave.",
"38 e. 51st st.", "328 lenox ave.", "1588 york ave.", "1460 first ave.",
"80 montague st.", "151 hudson st.", "106 bayard st.", "144 second ave.",
"689 ninth ave.", "107th fl .", "205 e. 49th st.", "122 e. 17th st.",
"953 second ave.", "401 s. 6th st.", "3300 las vegas blvd. s.",
"3700 w. flamingo rd.", "3799 las vegas blvd. s.", "3700 w. flamingo rd.",
"202 e. fremont st.", "3300 las vegas blvd. s.", "4750 w. sahara ave.",
"3595 las vegas blvd. s.", "3145 las vegas blvd. s.", "3400 las vegas blvd. s.",
"3200 las vegas blvd. s.", "3925 paradise rd.", "355 convention center dr.",
"3500 las vegas blvd. s.", "128 e. fremont st.", "129 fremont st.",
"3645 las vegas blvd. s.", "3799 las vegas blvd. s.", "103 w. paces ferry rd.",
"659 peachtree st.", "1134 euclid ave.", "1437 virginia ave.",
"56 e. andrews dr. nw", "375 14th st.", "2911 s. pharr court",
"4274 peachtree rd.", "5975 roswell rd.", "4199 paces ferry rd.",
"1021 cobb pkwy . se", "1215 powers ferry rd.", "70 w. paces ferry rd.",
"4279 roswell rd.", "1029 edgewood ave. se", "600 ponce de leon ave.",
"1655 mclendon ave.", "1031 peachtree st. ne", "1087 green st.",
"171 mcdonough blvd.", "2905 buford hwy .", "3675 satellite blvd.",
"790 ponce de leon ave.", "2970 cobb pkwy .", "1248 clairmont rd.",
"4427 roswell rd.", "3525 mall blvd.", "1031 ponce de leon ave.",
"303 peachtree st. ne", "1248 clairmont rd.", "3060 peachtree rd.",
"1495 chattahoochee ave. nw", "4330 peachtree rd.", "3391 peachtree rd. ne",
"2118 n. decatur rd.", "519 e. paces ferry rd.", "200 14th st. nw",
"3330 piedmont rd.", "764 marietta st. nw", "774 ponce de leon ave. ne",
"70 w. crossville rd.", "220 sandy springs circle", "3172 peachtree rd. ne",
"2315 clement st.", "2298 market st.", "423 columbus ave.", "240 columbus ave.",
"1805 haight st.", "2217 union st.", "522 jones st.", "3221 mission st.",
"1283 ninth ave.", "1550 california st.", "1500 church st.",
"1582 folsom st.", "333 bush st.", "515 valencia st.", "288 noe st.",
"2889 mission st.", "2209 polk st.", "2225 irving st.", "3355 geary st.",
"1322 grant st.", "631 larkin st.", "2817 24th st.", "300 de haro st.",
"1328 18th st.", "584 valencia st.", "1517 polk st.", "400 waller st.",
"3108 16th st.", "1870 fillmore st.", "8 sixth st.", "201 ivy st.",
"2141 polk st.", "435 s. la cienega blvd.", "12224 ventura blvd.",
"701 stone canyon rd.", "624 s. la brea ave.", "2709 main st.",
"6703 melrose ave.", "8358 sunset blvd.", "23725 w. malibu rd.",
"9560 dayton way", "903 n. la cienega blvd.", "8284 melrose ave.",
"8638 w. third st.", "129 n. la cienega blvd.", "9001 santa monica blvd.",
"5955 melrose ave.", "1001 n. alameda st.", "12969 ventura blvd.",