-
Notifications
You must be signed in to change notification settings - Fork 1
/
01_get_check_data.Rmd
1193 lines (951 loc) · 31.6 KB
/
01_get_check_data.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "01_get_check_data"
author: "Ross Gayler"
date: "2020-12-06"
output: workflowr::wflow_html
editor_options:
chunk_output_type: console
markdown:
wrap: 72
---
```{r setup}
library(here)
library(magrittr)
library(dplyr)
library(stringr)
library(vroom)
library(skimr)
library(knitr)
```
# Introduction
Read the data, characterise it to understand it, and check for possible
gotchas.
This project uses historical voter registration data from the [North
Carolina State Board of Elections](https://www.ncsbe.gov/). This
information is made publicly available in accordance with [North
Carolina state
law](https://s3.amazonaws.com/dl.ncsbe.gov/ReadMe_PUBLIC_DATA.txt). The
[Voter Registration Data
page](https://www.ncsbe.gov/results-data/voter-registration-data) links
to a [folder of Voter Registration
snapshots](https://dl.ncsbe.gov/index.html?prefix=data/Snapshots/),
which contains the snapshot data files and a [metadata file describing
the layout of the snapshot data
files](https://s3.amazonaws.com/dl.ncsbe.gov/data/Snapshots/layout_VR_Snapshot.txt).
At the time of writing the snapshot files cover the years 2005 to 2020
with at least one snapshot per year. The files are [ZIP
compressed](https://en.wikipedia.org/wiki/ZIP_(file_format)) and
relatively large, with the smallest being 572 MB after compression.
The snapshots contains many columns that are irrelevant to this project
and/or prohibited under Australian privacy law (e.g. political
affiliation, race). We initially read *all* the columns, because that
may help debugging the inevitable problems reading the data. Later the
data set will be restricted to the essential columns for the project.
We use only one snapshot file
([VR_Snapshot_20051125.zip](https://s3.amazonaws.com/dl.ncsbe.gov/data/Snapshots/VR_Snapshot_20051125.zip))
because this project does not investigate linkage of records across
time. We chose the oldest snapshot (2005) because it is the smallest and
the contents are the most out of date, minimising the current
information made available. Note that this project will not generate any
information that is not already directly, publicly available from NCSBE.
# Read data
The snapshot ZIP file was downloaded, uncompressed (5.7 GB), then
compressed in [XZ format](https://en.wikipedia.org/wiki/XZ_Utils) to
minimise the size. The compressed snapshot file and the metadata file
are stored in the `data` directory.
```{r}
raw_file <- here::here("data", "VR_20051125.txt.xz") # raw input file
```
The cleaned data is stored as an [`fst`
format](https://www.fstpackage.org/) file in the `output` directory.
```{r}
d_fst <- here::here("output", "d.fst") # temporary data file
clean_fst <- here::here("output", "clean.fst") # parsed and cleaned data as a dataframe
```
The data is tab-separated, not fixed-width as you might reasonably think
from reading the metadata. The field widths (interpreted as maximum
lengths) in the metadata are not accurate. Some fields contain values
longer than the stated width.
Inspection of the raw data shows that the character fields are unquoted.
However, at least one character value contains a double-quote character,
which has the potential to confuse the parsing if it is looking for
quoted values.
```{r eval=FALSE}
d <- vroom::vroom( #read raw data; let vroom guess the field types
raw_file,
delim = "\t", # assume that fields are *only* delimited by tabs
col_names = TRUE, # use the column names on the first line of data
na = "", # missing fields are empty string or whitespace only (see trim_ws argument)
quote = "", # don't allow for quoted strings
comment = "", # don't allow for comments
trim_ws = TRUE, # trim leading and trailing whitespace
escape_double = FALSE, # assume no escaped quotes
escape_backslash = FALSE # assume no escaped backslashes
)
fst::write_fst(d, d_fst, compress = 100) # save data frame (cheap-skate caching)
```
```{r}
d <- fst::read_fst(d_fst) %>% tibble::as_tibble() # get cached data
dim(d)
```
- Correct number of data rows extracted (external line count of input
file = 8,003,294)
# Characterise data (all records)
Take a very quick look at everything then concentrate on the columns
that have a chance of being useful.
```{r}
glimpse(d)
skimr::skim(d)
```
- The warning messages from `skim()` indicate that a handful of rows
contain unexpected characters. If they are in rows we use they will
have to be loacted and dealt with.
## county_id & county_desc
`county_id`: County identification number\
`county_desc`: County description
```{r}
summary(d$county_id)
table(d$county_id)
```
- Never missing
- Integer 1 .. 100
```{r}
table(d$county_desc)
```
- Never missing
- 100 unique values
They look reasonable, to the extent that I can tell without knowing
anything about the counties.
## voter_reg_num
`voter_reg_num`: Voter registration number (unique by county)
```{r}
table(d$voter_reg_num) %>% head(12)
table(d$voter_reg_num) %>% tail(12)
summary(as.integer(d$voter_reg_num))
d$voter_reg_num %>% stringr::str_length() %>% table(useNA = "ifany")
```
- \~2.7M unique values
- Never missing
- Integer 0 .. \~1,000M (as strings)
- Looks like they should be 12-digit integers with leading zeroes
- Exactly one observation is short
Look at the record with the short value.
```{r}
d %>%
dplyr::filter(stringr::str_length(voter_reg_num) < 12) %>%
dplyr::select(county_id, voter_reg_num, status_cd, voter_status_desc, reason_cd, voter_status_reason_desc) %>%
knitr::kable()
```
- There is only one short value which can be ignored because it will
later be excluded from the data set because of the observation's
status -not active. (I intend to later restrict the data set to only
active voters because, to the greatest extent possible, I want to
have no duplicate records in the data used for the analyses.)
Check whether `county_id x voter_reg_num` is unique, as claimed.
```{r}
d %>%
dplyr::select(county_id, voter_reg_num) %>%
dplyr::mutate(id = stringr::str_c(as.character(county_id), ".", voter_reg_num)) %>%
dplyr::count(id) %>%
with(table(n))
```
- `county_id x voter_reg_num` is unique, even including observations
flagged as duplicates.
## ncid
`ncid`: North Carolina identification number (NCID) of voter
- Always missing
That's a shame. It would have been useful.
## status_cd & voter_status_desc
`status_cd`: Status code for voter registration\
`voter_status_desc`: Status code description
```{r}
table(d$status_cd, useNA = "always")
table(d$voter_status_desc, useNA = "always")
```
- 5 unique nonmissing values
- 2 records with missing values
- \~4.9M active records
## reason_cd & voter_status_reason_desc
`reason_cd`: Reason code for voter registration status\
`voter_status_reason_desc`: Reason code description
```{r}
table(d$reason_cd, useNA = "always")
table(d$voter_status_reason_desc, useNA = "always")
```
- 26 unique nonmissing values
- 238 records with missing values
- \~4.1M verified records
Look at the relationship between status and status reason.
```{r}
table(
stringr::str_trunc(d$voter_status_reason_desc, 25),
stringr::str_trunc(d$voter_status_desc, 8),
useNA = "always"
)
```
- voter_status_desc == "ACTIVE" & voter_status_reason_desc ==
"VERIFIED"
- Most likely to be error free (based on common-sense
interpretation of the labels)
- \~4.1M observations
## Name standardisation
Identify any oddities about the name fields that might benefit from
standardisation.
I will do this on all the rows, not just the subset to be analysed,
because I expect the oddities to be much the same independently of
whether I will exclude the rows from the analyses and the larger sample
size will be helpful in spotting rare problems.
I will look at the three name fields concurrently because I expect the
oddities to be similar across the name fields.
- `last_name`: Voter last name
- `first_name`: Voter first name
- `midl_name`: Voter middle name
Look for possible anomalies in names.
### Name missing
```{r}
d %>% with(table(is.na(last_name)))
d %>% with(table(is.na(first_name)))
d %>% with(table(is.na(midl_name)))
```
- A small fraction of last and first names are missing. We don't
expect them to be missing.
- A significant fraction of middle names are missing. This is expected
as middle names are not mandatory.
Look at the records missing last or first names to see if there is some
explanation for their absence.
```{r}
# last name missing
d %>%
dplyr::filter(is.na(last_name)) %>%
dplyr::select(
first_name, midl_name, name_sufx_cd,
sex, age,
house_num, street_name,
voter_status_desc, voter_status_reason_desc
) %>%
dplyr::arrange(voter_status_desc, voter_status_reason_desc, first_name) %>%
knitr::kable()
```
- All the voters missing `last_name` are REMOVED. Perhaps it's a
side-effect of the removal process.
```{r}
# first name missing
d %>%
dplyr::filter(is.na(first_name)) %>%
dplyr::select(
last_name, midl_name, name_sufx_cd,
sex, age,
house_num, street_name,
voter_status_desc, voter_status_reason_desc
) %>%
dplyr::arrange(voter_status_desc, voter_status_reason_desc, midl_name) %>%
knitr::kable()
```
- Most are REMOVED, but some are ACTIVE and VERIFIED. That suggests
the data entry for this record is done *after* verification.
- Some appear to have the first name in the middle name field, e.g. (F
M L) ("" "BRENDA" "PARRISH"), ("" "ALEXIS" "BULLARD")
- Some appear to have first and middle names appended to the last
name, e.g. (F M L) ("" "" "JONES LARRY MALLOR"), ("" ""
"AMATO,KATHERINE,M")
- Some are missing all the names!
- Some appear to be test data, e.g. last name = XXX or "NEW TEST"
There are *very* few records missing first name or last name, and most
of them are REMOVED status. The easiest thing to do is just get rid of
those records.
**Exclude records with missing first or last name**
### Check for lower-case letters.
```{r}
d %>% dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "[a-z]"))
d %>% dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "[a-z]"))
d %>% dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "[a-z]"))
```
- 243 names with lower case letters.
- Occur in last, first, and middle names.
- Associated with particles where there would optionally be a space,
e.g. De VANE.
**Map all letters to upper case**
### Check for digits
```{r}
d %>% dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "[0-9]"))
d %>% dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "[0-9]"))
d %>% dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "[0-9]"))
```
- Zero substituted for O, e.g. J0HNSON, BURT0N
- Some are obviously generation suffixes, e.g. ARGUS 4TH, LEAK 111
(should be LEAK III)
- Some are poor parsing into fields, e.g. MV 5/17/95 , MIZELLE25248249
Look at the digits individually.
### Check for zero
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "0"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "0"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "0"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 270 names with zero
- Occur in last, first, and middle names.
- Most are zero substituted for O, e.g. J0HNSON, BURT0N
- Some are pure numeric, e.g. 0, 01
- Some are names with concatenated numeric, e.g. WAYNE030986,
WRIGHT2106
**Map zero to O if name contains at least one letter and no digits 1-9**
### Check for one
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "1"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "1"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "1"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 186 names with one
- Occur in last, first, and middle names.
- Most are 1 substituted for I in generation suffix, e.g. COX 1V, CARR
111
- Some are pure numeric, e.g. 01, 971
- Some are wrongly parsed, e.g. MV 5/17/95
- Some are names with concatenated numeric, e.g. LYNN2513, WRIGHT2106
**Delete generation suffixes where possible**
### Check for two
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "2"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "2"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "2"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 15 names with two
- Some are pure numeric, e.g. 205, 328
- Some are names with concatenated numeric, e.g. LYNN1820, WRIGHT2106
### Check for three
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "3"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "3"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "3"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 14 names with three
- Some are pure numeric, e.g. 103, 328
- Some are generation suffixes, e.g. 3RD., MACK 3RD
- Some are names with concatenated numeric, e.g. LEE3708, SCOTT3450
### Check for four
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "4"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "4"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "4"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 19 names with four
- Some are pure numeric, e.g. 4625, 4932
- Some are generation suffixes, e.g. ARGUS 4TH, MCREE 4
- Some are names with concatenated numeric, e.g. DALE401, SCOTT3450
- Some are intrusions in names, e.g. FR4ANK, MICHA4EL
### Check for five
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "5"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "5"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "5"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 20 names with five
- Some are pure numeric, e.g. 205, 2205
- Some are generation suffixes, e.g. (NMN)5TH
- Some are names with concatenated numeric, e.g. DALE401, SCOTT3450
- Some are wrongly parsed, e.g. MV 5/17/95
- Some are intrusions in names, e.g. FR4ANK, MICHA4EL
- Some are substitution of 5 for S e.g. ALBER5TSON
### Check for six
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "6"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "6"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "6"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 9 names with six
- Some are pure numeric, e.g. 6, 4625
- Some are names with concatenated nmeric, e.g. MICHAEL146, MICHAEL146
### Check for seven
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "7"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "7"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "7"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 12 names with seven
- Some are pure numeric, e.g. 491715, 971
- Some are names with concatenated numeric, e.g. DALE401, SCOTT3450
- Some are wrongly parsed, e.g. MV 5/17/95
- Some are intrusions in names, e.g. JOYCE701, LOUIS7100
### Check for eight
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "8"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "8"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "8"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 11 names with eight
- Some are pure numeric, e.g. 328, 8017
- Some are names with concatenated numeric, e.g. LEE3708, LYNN1820
- Some are intrusions in names, e.g. J8IMMIE
- Some might be substitution of 8 for SE e.g. BEA LOUI8
### Check for nine
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "9"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
x <- d %>%
dplyr::select(first_name) %>%
dplyr::filter(stringr::str_detect(first_name, "9"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(first_name) %>%
dplyr::pull(first_name)
x <- d %>%
dplyr::select(midl_name) %>%
dplyr::filter(stringr::str_detect(midl_name, "9"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::arrange(midl_name) %>%
dplyr::pull(midl_name)
```
- 10 names with nine
- Some are pure numeric, e.g. 971, 4932
- Some are names with concatenated numeric, e.g. ANDERSON9104576,
WAYNE030986
- Some are wrongly parsed, e.g. MV 5/17/95
- Some are intrusions in names, e.g. LO9UIS
### Check for whitespace.
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "\\s"))
dim(x)
x %>%
dplyr::slice_head(n = 100) %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
```
- \~7k names with whitespace
- Some whitespace is because of prefixes, e.g. DE COSTA, VAN DYKE
- Some whitespace is instead of a hyphen, e.g. BROWN MAY, JONES MBIYA
- Some whitespace is incorrectly inserted, e.g LI NDSEY
- Some whitespace is probably variable between people, e.g. MC ADAMS,
MC INTOSH
- Some whitespace is instead of a single quote, e.g. O KELLY, O NEAL
**Map whitespace to empty string**
### Check for hyphens
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "-"))
dim(x)
x %>%
dplyr::slice_head(n = 100) %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
```
- \~21k names with hyphens
- Look like legitimately hyphenated names
**Map hyphen to empty string**
### Check for single quotes
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "'"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::slice_head(n = 100) %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
```
- \~5k names with single quotes
- Most look like correct names, e.g. O'NEAL, D'AGOSTINO
- Some are suspect, e.g. BONE', BOVA'
**Map single quote to empty string**
### Check for double quotes
```{r}
d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "\""))
```
- 1 name with double quotes
- The backslash in LA"BEE was probably inserted automatically when the
data was exported
- That name probably should have been LA'BEE
**Map all double quotes to single quotes**
### Check for other characters (1)
```{r}
x <- d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "[^-\\s'\"a-zA-Z]"))
dim(x)
x %>%
dplyr::distinct() %>%
dplyr::slice_head(n = 100) %>%
dplyr::arrange(last_name) %>%
dplyr::pull(last_name)
```
- 73 rows with other characters
- five zero period back-tick slash back-slash asterisk comma tilde one
percent underscore
Look at those in more detail.
### Check for period
```{r}
d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "\\."))
```
- 11 names with period
- Most are legitimate abbreviation of SAINT although spacing is
inconsistent, e.g. ST.JOHN, ST. JOHN
- Some are legitimate abbreviation of Junior, which should be in the
`name_sufx_cd` field.
**Map period to empty string**
**Move suffix to suffix field**
### Check for comma
```{r}
d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, ","))
```
- 2 names with comma
- Both are when a suffix has been incorrectly included in `last_name`
**Map comma to empty string**
**Move suffix to suffix field**
### Check for asterisk
```{r}
d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "\\*"))
```
- 7 names with asterisk
- Asterisk substituted for single quote, e.g. O*MASTERS, D*AMICO
**Map asterisk to empty string**
### Check for slash
```{r}
d %>%
dplyr::select(last_name, first_name, sex) %>%
dplyr::filter(stringr::str_detect(last_name, "/"))
```
- 13 names with slash
- Being used equivalently to hyphen. (The fact that they are all
female suggests they might be women hyphenating their names on
marriage.)
**Map slash to empty string**
### Check for backslash
```{r}
d %>%
dplyr::select(last_name, first_name, sex) %>%
dplyr::filter(stringr::str_detect(last_name, "\\\\"))
```
- 3 names with backslash
- No obvious reason for inclusion
**Map backslash to empty string**
### Check for back-tick
```{r}
d %>%
dplyr::select(last_name, first_name, sex) %>%
dplyr::filter(stringr::str_detect(last_name, "`"))
```
- 4 names with back-tick
- No obvious reason for inclusion
**Map back-tick to empty string**
### Check for tilde
```{r}
d %>%
dplyr::select(last_name, first_name, sex) %>%
dplyr::filter(stringr::str_detect(last_name, "~"))
```
- 1 name with tilde
- Being used equivalent to single quote
**Map tilde to empty string**
### Check for underscore
```{r}
d %>%
dplyr::select(last_name, first_name, sex) %>%
dplyr::filter(stringr::str_detect(last_name, "_"))
```
- 1 name with underscore
- Being used equivalent to hyphen
**Map underscore to empty string**
### Check for percent
```{r}
d %>%
dplyr::select(last_name, first_name, sex) %>%
dplyr::filter(stringr::str_detect(last_name, "%"))
```
- 1 name with percent
- Being used equivalent to hyphen
**Map percent to empty string**
### Check for other characters (2)
```{r}
d %>%
dplyr::select(last_name) %>%
dplyr::filter(stringr::str_detect(last_name, "[^-\\s'\"a-zA-Z015\\.,\\*/\\\\`~_%]"))
```
**UP TO HERE**
Look at those in more detail.
Look at frequencies of names.
```{r}
d %>%
dplyr::select(last_name) %>%
dplyr::count(last_name, sort = TRUE)
```
## name_sufx_cd
`name_sufx_cd`: Voter name suffix
```{r}
d %>% dplyr::select(name_sufx_cd) %>% skimr::skim()
table(d$name_sufx_cd, useNA = "ifany")
```
# Clean name variables
The aggregated cleaning suggestions are:
+-------------+-------------+-------------+-------------+-------------+
| Issue | `last_name` | ` | `midl_name` | Action |