-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_nibrs_summary_stats.R
1263 lines (1171 loc) · 66.6 KB
/
make_nibrs_summary_stats.R
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
source("_common.R")
# Offender ----------------------------------------------------------------
offender_files <- list.files("F:/ucr_data_storage/clean_data/NIBRS",
pattern = "offender.*rds$",
full.names = TRUE
)
offender_files
offender_final <- data.frame(
year = 1991:2023,
number_of_agencies = NA,
percent_unknown_age = NA,
median_age = NA,
mean_age = NA,
percent_male = NA,
percent_female = NA,
percent_unknown_sex = NA,
percent_unknown_race = NA,
percent_asian = NA,
percent_black = NA,
percent_american_indian = NA,
percent_white = NA,
percent_native_hawaiian = NA,
percent_hispanic = NA,
percent_not_hispanic = NA,
percent_ethnicity_unknown = NA
)
for (file in offender_files) {
offender_temp <- readRDS(file)
offender_temp$age_of_offender[offender_temp$age_of_offender %in% "unknown"] <- NA
offender_temp$age_of_offender[offender_temp$age_of_offender %in% "over 98 years old"] <- 99
offender_temp$age_of_offender <- as.numeric(offender_temp$age_of_offender)
offender_temp$sex_of_offender[is.na(offender_temp$sex_of_offender)] <- "unknown"
offender_temp$race_of_offender[is.na(offender_temp$race_of_offender)] <- "unknown"
offender_temp$ethnicity_of_offender[is.na(offender_temp$ethnicity_of_offender)] <- "unknown"
offender_final$number_of_agencies[offender_final$year %in% unique(offender_temp$year)] <- length(unique(offender_temp$ori))
# Age
offender_final$median_age[offender_final$year %in% unique(offender_temp$year)] <- median(offender_temp$age_of_offender, na.rm = TRUE)
offender_final$mean_age[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$age_of_offender, na.rm = TRUE)
offender_final$percent_unknown_age[offender_final$year %in% unique(offender_temp$year)] <- mean(is.na(offender_temp$age_of_offender))
# Sex
offender_final$percent_male[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$sex_of_offender %in% "male")
offender_final$percent_female[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$sex_of_offender %in% "female")
offender_final$percent_unknown_sex[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$sex_of_offender %in% "unknown")
# Race
offender_final$percent_unknown_race[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$race_of_offender %in% "unknown")
offender_final$percent_asian[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$race_of_offender %in% "asian")
offender_final$percent_black[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$race_of_offender %in% "black")
offender_final$percent_american_indian[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$race_of_offender %in% "american indian/alaskan native")
offender_final$percent_white[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$race_of_offender %in% "white")
offender_final$percent_native_hawaiian[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$race_of_offender %in% "native hawaiian or other pacific islander")
# Ethnicity
offender_final$percent_hispanic[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$ethnicity_of_offender %in% "hispanic or latino")
offender_final$percent_not_hispanic[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$ethnicity_of_offender %in% "not hispanic or latino")
offender_final$percent_ethnicity_unknown[offender_final$year %in% unique(offender_temp$year)] <- mean(offender_temp$ethnicity_of_offender %in% "unknown")
message(file)
message("Age")
print(sort(unique(offender_temp$age_of_offender)))
message("Sex")
print(sort(unique(offender_temp$sex_of_offender)))
message("Race")
print(sort(unique(offender_temp$race_of_offender)))
message("Ethnicity")
print(sort(unique(offender_temp$ethnicity_of_offender)))
message("\n\n\n")
}
offender_final
saveRDS(offender_final, "data/nibrs_summary_stats/nibrs_offender_summary_stats.rds")
# Offense -----------------------------------------------------------------
offense_files <- list.files("F:/ucr_data_storage/clean_data/NIBRS",
pattern = "offense.*rds$",
full.names = TRUE
)
offense_files
offense_final <- data.frame(
year = 1991:2023,
number_of_agencies = NA,
number_of_offenses = NA,
number_murders = NA,
percent_offense_completed = NA,
percent_burglary_force = NA,
percent_burglary_no_force = NA,
percent_with_bias = NA,
percent_at_home = NA,
percent_murders_gun = NA,
percent_murders_handgun = NA,
percent_murders_unarmed = NA,
percent_murders_knife = NA,
percent_murders_other_weapon = NA,
percent_rifle_automatic = NA,
percent_shotgun_automatic = NA,
percent_other_firearm_automatic = NA,
percent_firearm_type_not_stated_automatic = NA,
percent_handgun_automatic = NA
)
offense_first_year <- data.frame()
location_first_year <- data.frame()
bias_motivation_first_year <- data.frame()
for (file in offense_files) {
offense_temp <- readRDS(file) %>% distinct(unique_incident_id,
ucr_offense_code,
.keep_all = TRUE
)
offense_temp$bias_motivation[is.na(offense_temp$bias_motivation)] <- "no bias motivation"
offense_final$number_of_agencies[offense_final$year %in% unique(offense_temp$year)] <- length(unique(offense_temp$ori))
offense_final$number_of_offenses[offense_final$year %in% unique(offense_temp$year)] <- nrow(offense_temp)
offense_final$number_murders[offense_final$year %in% unique(offense_temp$year)] <-
nrow(offense_temp[offense_temp$ucr_offense_code %in% "murder/nonnegligent manslaughter", ])
offense_final$percent_offense_completed[offense_final$year %in% unique(offense_temp$year)] <- mean(offense_temp$offense_attempted_or_completed %in% "completed")
offense_final$percent_burglary_force[offense_final$year %in% unique(offense_temp$year)] <- mean(offense_temp$method_of_entry[offense_temp$ucr_offense_code %in% "burglary/breaking and entering"] %in% "force")
offense_final$percent_burglary_no_force[offense_final$year %in% unique(offense_temp$year)] <- mean(offense_temp$method_of_entry[offense_temp$ucr_offense_code %in% "burglary/breaking and entering"] %in% "no force")
offense_final$percent_with_bias[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$bias_motivation %in% c(
c(
"anti-white",
"anti-black or african american",
"anti-american indian or alaskan native",
"anti-asian",
"anti-multiple races, group",
"anti-jewish",
"anti-catholic",
"nti-protestant",
"anti-islamic (muslim)",
"anti-other religion",
"anti-multiple religions, group",
"anti-atheism/agnosticism",
"anti-arab",
"anti-hispanic or latino",
"anti-other race, ethnicity, ancestry, or national origin",
"anti-gay (male)",
"anti-lesbian (female)",
"anti-lesbian, gay, bisexual, or transgender (mixed group)",
"anti-heterosexual",
"anti-bisexual",
"anti-native hawaiian or other pacific islander",
"anti-church of jesus christ (mormon)",
"anti-jehovahs witness",
"anti-physical disability",
"anti-mental disability",
"anti-male",
"anti-female",
"anti-transgender",
"anti-gender non-conforming",
"anti-eastern orthodox (greek, russian, other)",
"anti-other christian",
"anti-buddhist",
"anti-hindu",
"anti-sikh"
)
))
offense_final$percent_at_home[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$location_type %in% "residence/home")
# Weapon
offense_final$percent_murders_gun[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$type_weapon_force_involved_1[offense_temp$ucr_offense_code %in% "murder/nonnegligent manslaughter"] %in%
c(
"rifle",
"handgun",
"shotgun",
"firearm (type not stated)",
"other firearm"
))
offense_final$percent_murders_handgun[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$type_weapon_force_involved_1[offense_temp$ucr_offense_code %in% "murder/nonnegligent manslaughter"] %in%
c("handgun"))
offense_final$percent_murders_unarmed[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$type_weapon_force_involved_1[offense_temp$ucr_offense_code %in% "murder/nonnegligent manslaughter"] %in%
c("personal weapons (hands, feet, teeth, etc.)"))
offense_final$percent_murders_knife[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$type_weapon_force_involved_1[offense_temp$ucr_offense_code %in% "murder/nonnegligent manslaughter"] %in%
c("lethal cutting instrument (knife, ice pick, screwdriver, ax, etc.)"))
offense_final$percent_murders_other_weapon[offense_final$year %in% unique(offense_temp$year)] <-
mean(!offense_temp$type_weapon_force_involved_1[offense_temp$ucr_offense_code %in% "murder/nonnegligent manslaughter"] %in%
c(
"lethal cutting instrument (knife, ice pick, screwdriver, ax, etc.)",
"personal weapons (hands, feet, teeth, etc.)",
"rifle",
"handgun",
"shotgun",
"firearm (type not stated)",
"other firearm"
))
# Automatic
offense_final$percent_rifle_automatic[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$automatic_weapon_indicator_1[offense_temp$type_weapon_force_involved_1 %in% "rifle"] %in% "automatic weapon")
offense_final$percent_shotgun_automatic[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$automatic_weapon_indicator_1[offense_temp$type_weapon_force_involved_1 %in% "shotgun"] %in% "automatic weapon")
offense_final$percent_other_firearm_automatic[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$automatic_weapon_indicator_1[offense_temp$type_weapon_force_involved_1 %in% "other firearm"] %in% "automatic weapon")
offense_final$percent_firearm_type_not_stated_automatic[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$automatic_weapon_indicator_1[offense_temp$type_weapon_force_involved_1 %in% "firearm (type not stated)"] %in% "automatic weapon")
offense_final$percent_handgun_automatic[offense_final$year %in% unique(offense_temp$year)] <-
mean(offense_temp$automatic_weapon_indicator_1[offense_temp$type_weapon_force_involved_1 %in% "handgun"] %in% "automatic weapon")
# Get first year offense is reported
offenses_offense_temp <- offense_temp %>%
distinct(ucr_offense_code) %>%
mutate(year = unique(offense_temp$year)) %>%
filter(!ucr_offense_code %in% offense_first_year$ucr_offense_code)
offense_first_year <-
offense_first_year %>%
bind_rows(offenses_offense_temp) %>%
arrange(
year,
ucr_offense_code
)
# Get first year location is reported
locations_offense_temp <- offense_temp %>%
distinct(location_type) %>%
mutate(year = unique(offense_temp$year)) %>%
filter(!location_type %in% location_first_year$location_type)
location_first_year <-
location_first_year %>%
bind_rows(locations_offense_temp) %>%
arrange(
year,
location_type
)
# Get first year drug is reported
bias_motivation_first_year_temp <- offense_temp %>%
distinct(bias_motivation) %>%
mutate(year = unique(offense_temp$year)) %>%
filter(!bias_motivation %in% bias_motivation_first_year$bias_motivation)
bias_motivation_first_year <-
bias_motivation_first_year %>%
bind_rows(bias_motivation_first_year_temp) %>%
arrange(
year,
bias_motivation
)
message(file)
message("Bias")
print(sort(unique(offense_temp$bias_motivation)))
message("Location")
print(sort(unique(offense_temp$location_type)))
message("Offense completed")
print(sort(unique(offense_temp$offense_attempted_or_completed)))
message("Crimes")
print(sort(unique(offense_temp$ucr_offense_code)))
message("Burglary entry type")
print(sort(unique(offense_temp$method_of_entry[offense_temp$ucr_offense_code %in% "burglary/breaking and entering"])))
message("Murder weapon")
print(sort(unique(offense_temp$type_weapon_force_involved_1[offense_temp$ucr_offense_code %in% "murder/nonnegligent manslaughter"])))
message("\n\n\n")
}
offense_final
offense_first_year
location_first_year
bias_motivation_first_year
saveRDS(offense_final, "data/nibrs_summary_stats/nibrs_offense_summary_stats.rds")
saveRDS(offense_first_year, "data/nibrs_summary_stats/nibrs_offense_first_year.rds")
saveRDS(location_first_year, "data/nibrs_summary_stats/nibrs_location_first_year.rds")
saveRDS(bias_motivation_first_year, "data/nibrs_summary_stats/bias_motivation_first_year.rds")
# Victim ----------------------------------------------------------------
victim_files <- list.files("F:/ucr_data_storage/clean_data/NIBRS",
pattern = "victim.*rds$",
full.names = TRUE
)
victim_files
officer_victim_offense_first_year <- data.frame()
victim_type_first_year <- data.frame()
victim_offense_first_year <- data.frame()
victim_relationship_first_year <- data.frame()
victim_agg_assault_circumstance_first_year <- data.frame()
officer_victim_relationship_first_year <- data.frame()
victim_final <- data.frame(
year = 1991:2023,
number_of_agencies = NA,
percent_unknown_age = NA,
median_age = NA,
mean_age = NA,
percent_male = NA,
percent_female = NA,
percent_unknown_sex = NA,
percent_unknown_race = NA,
percent_asian = NA,
percent_black = NA,
percent_american_indian = NA,
percent_white = NA,
percent_native_hawaiian = NA,
percent_hispanic = NA,
percent_not_hispanic = NA,
percent_ethnicity_unknown = NA,
percent_victim_officer = NA,
number_victim_officer = NA,
percent_victim_individual = NA,
percent_victim_society_public = NA,
percent_victim_business = NA,
percent_resident_status_resident = NA,
percent_resident_status_nonresident = NA,
percent_resident_status_unknown = NA,
percent_resident_status_resident_officer = NA,
percent_resident_status_nonresident_officer = NA,
percent_resident_status_unknown_officer = NA,
percent_assault_no_injury = NA,
percent_assault_minor_injury = NA,
percent_assault_major_injury = NA,
percent_assault_spouse = NA,
number_murders_first_offense = NA,
number_murders_all_offenses = NA,
number_sex_offenses_first_offense = NA,
number_sex_offenses_all_offenses = NA,
number_motor_vehicle_theft_first_offense = NA,
number_motor_vehicle_theft_all_offenses = NA,
number_vandalism_first_offense = NA,
number_vandalism_all_offenses = NA,
number_theft_first_offense = NA,
number_theft_all_offenses = NA
)
for (file in victim_files) {
victim_temp <- readRDS(file)
victim_temp$age_of_victim[victim_temp$age_of_victim %in% "unknown"] <- NA
victim_temp$age_of_victim[victim_temp$age_of_victim %in% "over 98 years old"] <- 99
victim_temp$age_of_victim[victim_temp$age_of_victim %in% c(
"between 6 days and 1 year old (baby)",
"under 24 hours (neonate)",
"1 to 6 days old (newborn)"
)] <- 0
victim_temp$age_of_victim <- as.numeric(victim_temp$age_of_victim)
victim_temp$sex_of_victim[is.na(victim_temp$sex_of_victim)] <- "unknown"
victim_temp$race_of_victim[is.na(victim_temp$race_of_victim)] <- "unknown"
victim_temp$ethnicity_of_victim[is.na(victim_temp$ethnicity_of_victim)] <- "unknown"
victim_temp$resident_status_of_victim[is.na(victim_temp$resident_status_of_victim)] <- "unknown"
victim_temp$type_of_injury_1[is.na(victim_temp$type_of_injury_1)] <- "unknown"
victim_final$number_of_agencies[victim_final$year %in% unique(victim_temp$year)] <- length(unique(victim_temp$ori))
# Age
victim_final$median_age[victim_final$year %in% unique(victim_temp$year)] <-
median(victim_temp$age_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")], na.rm = TRUE)
victim_final$mean_age[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$age_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")], na.rm = TRUE)
victim_final$percent_unknown_age[victim_final$year %in% unique(victim_temp$year)] <-
mean(is.na(victim_temp$age_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")]))
# Sex
victim_final$percent_male[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$sex_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "male")
victim_final$percent_female[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$sex_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "female")
victim_final$percent_unknown_sex[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$sex_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "unknown")
# Race
victim_final$percent_unknown_race[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$race_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "unknown")
victim_final$percent_asian[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$race_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "asian")
victim_final$percent_black[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$race_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "black")
victim_final$percent_american_indian[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$race_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "american indian/alaskan native")
victim_final$percent_white[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$race_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "white")
victim_final$percent_native_hawaiian[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$race_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "native hawaiian or other pacific islander")
# Ethnicity
victim_final$percent_hispanic[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$ethnicity_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "hispanic or latino")
victim_final$percent_not_hispanic[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$ethnicity_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "not hispanic or latino")
victim_final$percent_ethnicity_unknown[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$ethnicity_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "unknown")
# Victim type
victim_final$percent_victim_officer[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$type_of_victim %in% "law enforcement officer")
victim_final$number_victim_officer[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$type_of_victim %in% "law enforcement officer")
victim_final$percent_victim_individual[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$type_of_victim %in% "individual")
victim_final$percent_victim_business[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$type_of_victim %in% "business")
victim_final$percent_victim_society_public[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$type_of_victim %in% "society/public")
# Resident status
victim_final$percent_resident_status_resident[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$resident_status_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "resident")
victim_final$percent_resident_status_nonresident[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$resident_status_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "nonresident")
victim_final$percent_resident_status_unknown[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$resident_status_of_victim[victim_temp$type_of_victim %in% c("individual", "law enforcement officer")] %in% "unknown")
# Resident status officer
victim_final$percent_resident_status_resident_officer[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$resident_status_of_victim[victim_temp$type_of_victim %in% "law enforcement officer"] %in% "resident")
victim_final$percent_resident_status_nonresident_officer[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$resident_status_of_victim[victim_temp$type_of_victim %in% "law enforcement officer"] %in% "nonresident")
victim_final$percent_resident_status_unknown_officer[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$resident_status_of_victim[victim_temp$type_of_victim %in% "law enforcement officer"] %in% "unknown")
victim_final$percent_resident_status_resident_officer[is.nan(victim_final$percent_resident_status_resident_officer)] <- 0
victim_final$percent_resident_status_nonresident_officer[is.nan(victim_final$percent_resident_status_nonresident_officer)] <- 0
victim_final$percent_resident_status_unknown_officer[is.nan(victim_final$percent_resident_status_unknown_officer)] <- 0
# Assault injury
victim_final$percent_assault_no_injury[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$type_of_injury_1[victim_temp$ucr_offense_code_1 %in%
c(
"assault offenses - aggravated assault",
"assault offenses - simple assault"
)] %in% "none")
victim_final$percent_assault_minor_injury[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$type_of_injury_1[victim_temp$ucr_offense_code_1 %in%
c(
"assault offenses - aggravated assault",
"assault offenses - simple assault"
)] %in% "apparent minor injuries")
victim_final$percent_assault_major_injury[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$type_of_injury_1[victim_temp$ucr_offense_code_1 %in%
c(
"assault offenses - aggravated assault",
"assault offenses - simple assault"
)] %in% c(
"apparent broken bones",
"severe laceration",
"possible internal injury",
"other major injury",
"loss of teeth",
"unconsciousness"
))
# Assault spouse
victim_final$percent_assault_spouse[victim_final$year %in% unique(victim_temp$year)] <-
mean(victim_temp$relation_of_vict_to_offender1[victim_temp$ucr_offense_code_1 %in%
c(
"assault offenses - aggravated assault",
"assault offenses - simple assault"
)] %in%
c(
"victim was boyfriend/girlfriend",
"victim was spouse",
"victim was ex-relationship (ex-boyfriend/ex-girlfriend)",
"victim was common-law spouse",
"victim was ex-spouse",
"victim was in a homosexual relationship with the offender"
))
# Number murders
victim_final$number_murders_first_offense[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% "murder/nonnegligent manslaughter")
victim_final$number_murders_all_offenses[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_2 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_3 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_4 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_5 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_6 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_7 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_8 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_9 %in% "murder/nonnegligent manslaughter" |
victim_temp$ucr_offense_code_10 %in% "murder/nonnegligent manslaughter")
# Number rapes
sex_offenses <- c(
"sex offenses - fondling (indecent liberties/child molest)",
"sex offenses - incest",
"sex offenses - rape",
"sex offenses - sexual assault with an object",
"sex offenses - sodomy",
"sex offenses - statutory rape"
)
victim_final$number_sex_offenses_first_offense[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% sex_offenses)
victim_final$number_sex_offenses_all_offenses[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% sex_offenses |
victim_temp$ucr_offense_code_2 %in% sex_offenses |
victim_temp$ucr_offense_code_3 %in% sex_offenses |
victim_temp$ucr_offense_code_4 %in% sex_offenses |
victim_temp$ucr_offense_code_5 %in% sex_offenses |
victim_temp$ucr_offense_code_6 %in% sex_offenses |
victim_temp$ucr_offense_code_7 %in% sex_offenses |
victim_temp$ucr_offense_code_8 %in% sex_offenses |
victim_temp$ucr_offense_code_9 %in% sex_offenses |
victim_temp$ucr_offense_code_10 %in% sex_offenses)
# Number motor vehicle theft
victim_final$number_motor_vehicle_theft_first_offense[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% "motor vehicle theft")
victim_final$number_motor_vehicle_theft_all_offenses[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_2 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_3 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_4 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_5 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_6 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_7 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_8 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_9 %in% "motor vehicle theft" |
victim_temp$ucr_offense_code_10 %in% "motor vehicle theft")
# Number vandalism
victim_final$number_vandalism_first_offense[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% "destruction/damage/vandalism of property")
victim_final$number_vandalism_all_offenses[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_2 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_3 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_4 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_5 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_6 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_7 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_8 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_9 %in% "destruction/damage/vandalism of property" |
victim_temp$ucr_offense_code_10 %in% "destruction/damage/vandalism of property")
# Number theft
theft_offenses <- c("larceny/theft offenses - all other larceny",
"larceny/theft offenses - pocket-picking",
"larceny/theft offenses - purse-snatching",
"larceny/theft offenses - shoplifting",
"larceny/theft offenses - theft from building",
"larceny/theft offenses - theft from coin-operated machine or device",
"larceny/theft offenses - theft from motor vehicle",
"larceny/theft offenses - theft of motor vehicle parts/accessories")
victim_final$number_theft_first_offense[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% theft_offenses)
victim_final$number_theft_all_offenses[victim_final$year %in% unique(victim_temp$year)] <-
sum(victim_temp$ucr_offense_code_1 %in% theft_offenses |
victim_temp$ucr_offense_code_2 %in% theft_offenses |
victim_temp$ucr_offense_code_3 %in% theft_offenses |
victim_temp$ucr_offense_code_4 %in% theft_offenses |
victim_temp$ucr_offense_code_5 %in% theft_offenses |
victim_temp$ucr_offense_code_6 %in% theft_offenses |
victim_temp$ucr_offense_code_7 %in% theft_offenses |
victim_temp$ucr_offense_code_8 %in% theft_offenses |
victim_temp$ucr_offense_code_9 %in% theft_offenses |
victim_temp$ucr_offense_code_10 %in% theft_offenses)
# Get first year victim type is reported
victim_type_victim_temp <- victim_temp %>%
distinct(type_of_victim) %>%
mutate(year = unique(victim_temp$year)) %>%
filter(!type_of_victim %in% victim_type_first_year$type_of_victim)
victim_type_first_year <-
victim_type_first_year %>%
bind_rows(victim_type_victim_temp) %>%
arrange(
year,
type_of_victim
)
# Get first year offense is reported for officer victims
officer_victim_offense_victim_temp <- victim_temp %>%
filter(type_of_victim %in% "law enforcement officer") %>%
count(ucr_offense_code_1) %>%
rename(number_of_victims = n) %>%
mutate(year = unique(victim_temp$year)) %>%
filter(!ucr_offense_code_1 %in% officer_victim_offense_first_year$ucr_offense_code_1)
officer_victim_offense_first_year <-
officer_victim_offense_first_year %>%
bind_rows(officer_victim_offense_victim_temp) %>%
arrange(
year,
ucr_offense_code_1
)
# Get first year offense is reported
victim_offense_victim_temp <- victim_temp %>%
count(ucr_offense_code_1) %>%
rename(number_of_victims = n) %>%
mutate(year = unique(victim_temp$year)) %>%
filter(!ucr_offense_code_1 %in% victim_offense_first_year$ucr_offense_code_1)
victim_offense_first_year <-
victim_offense_first_year %>%
bind_rows(victim_offense_victim_temp) %>%
arrange(
year,
ucr_offense_code_1
)
# Get first year relationship is reported
victim_relationship_first_year_temp <- victim_temp %>%
count(relation_of_vict_to_offender1) %>%
rename(number_of_victims = n) %>%
mutate(year = unique(victim_temp$year)) %>%
filter(!relation_of_vict_to_offender1 %in% victim_relationship_first_year$relation_of_vict_to_offender1)
victim_relationship_first_year <-
victim_relationship_first_year %>%
bind_rows(victim_relationship_first_year_temp) %>%
arrange(
year,
relation_of_vict_to_offender1
)
# Get first year relationship is reported for officers
officer_victim_relationship_first_year_temp <- victim_temp %>%
filter(type_of_victim %in% "law enforcement officer") %>%
count(relation_of_vict_to_offender1) %>%
rename(number_of_victims = n) %>%
mutate(year = unique(victim_temp$year)) %>%
filter(!relation_of_vict_to_offender1 %in% officer_victim_relationship_first_year$relation_of_vict_to_offender1)
officer_victim_relationship_first_year <-
officer_victim_relationship_first_year %>%
bind_rows(officer_victim_relationship_first_year_temp) %>%
arrange(
year,
relation_of_vict_to_offender1
)
# Get first year agg-assault circumstance is reported
victim_agg_assault_circumstance_first_year_temp <- victim_temp %>%
filter(!is.na(agg_assault_homicide_circumsta1)) %>%
count(agg_assault_homicide_circumsta1) %>%
rename(number_of_victims = n) %>%
mutate(year = unique(victim_temp$year)) %>%
filter(!agg_assault_homicide_circumsta1 %in% victim_agg_assault_circumstance_first_year$agg_assault_homicide_circumsta1)
victim_agg_assault_circumstance_first_year <-
victim_agg_assault_circumstance_first_year %>%
bind_rows(victim_agg_assault_circumstance_first_year_temp) %>%
arrange(
year,
agg_assault_homicide_circumsta1
)
message(file)
message("Age")
print(sort(unique(victim_temp$age_of_victim)))
message("Sex")
print(sort(unique(victim_temp$sex_of_victim)))
message("Race")
print(sort(unique(victim_temp$race_of_victim)))
message("Ethnicity")
print(sort(unique(victim_temp$ethnicity_of_victim)))
message("Resident status")
print(sort(unique(victim_temp$resident_status_of_victim)))
message("Injury")
print(sort(unique(victim_temp$type_of_injury_1)))
message("Type of victim")
print(sort(unique(victim_temp$type_of_victim)))
message("Offenses")
print(sort(unique(victim_temp$ucr_offense_code_1)))
message("\n\n\n")
}
victim_final
victim_type_first_year
officer_victim_offense_first_year
victim_offense_first_year
victim_relationship_first_year
victim_agg_assault_circumstance_first_year
officer_victim_relationship_first_year
saveRDS(victim_type_first_year, "data/nibrs_summary_stats/victim_type_first_year.rds")
saveRDS(officer_victim_offense_first_year, "data/nibrs_summary_stats/officer_victim_offense_first_year.rds")
saveRDS(victim_offense_first_year, "data/nibrs_summary_stats/victim_offense_first_year.rds")
saveRDS(victim_relationship_first_year, "data/nibrs_summary_stats/victim_relationship_first_year.rds")
saveRDS(victim_agg_assault_circumstance_first_year, "data/nibrs_summary_stats/victim_agg_assault_circumstance_first_year.rds")
saveRDS(officer_victim_relationship_first_year, "data/nibrs_summary_stats/officer_victim_relationship_first_year.rds")
saveRDS(victim_final, "data/nibrs_summary_stats/nibrs_victim_summary_stats.rds")
# Batch Header ----------------------------------------------------------------
batch_header_files <- list.files("F:/ucr_data_storage/clean_data/NIBRS",
pattern = "batch.*rds$",
full.names = TRUE
)
batch_header_files
batch_header <- data.frame()
for (file in batch_header_files) {
temp <- readRDS(file) %>%
mutate_if(is.character, tolower)
if (unique(temp$year) %in% 2000) {
temp$population <-
as.numeric(temp$last_population_1) +
as.numeric(temp$last_population_2) +
as.numeric(temp$last_population_3) +
as.numeric(temp$last_population_4) +
as.numeric(temp$last_population_5)
}
batch_header <-
batch_header %>%
bind_rows(temp)
message(file)
}
saveRDS(batch_header, "data/nibrs_summary_stats/batch_header_all_years.rds")
# Administrative ----------------------------------------------------------------
administrative_files <- list.files("F:/ucr_data_storage/clean_data/NIBRS",
pattern = "administrative.*rds$",
full.names = TRUE
)
administrative_files
administrative_final <- data.frame(
year = 1991:2023,
number_of_agencies = NA,
percent_cleared_exceptionally = NA,
percent_with_arrest = NA,
percent_with_arrest_or_cleared_exceptionally = NA,
mean_number_offense_segments = NA,
median_number_offense_segments = NA,
mean_number_victim_segments = NA,
median_number_victim_segments = NA,
mean_number_offender_segments = NA,
median_number_offender_segments = NA,
mean_number_arrestee_segments = NA,
median_number_arrestee_segments = NA,
most_common_hour = NA,
most_common_hour_excluding_midnight = NA,
most_common_hour_excluding_midnight_and_noon = NA,
least_common_hour = NA,
percent_hour_midnight = NA,
percent_hour_noon = NA,
percent_hour_unknown = NA,
percent_exceptional_offender_death = NA,
percent_exceptional_extradition_denied = NA,
percent_exceptional_juvenile_no_custody = NA,
percent_exceptional_prosecution_declined = NA,
percent_exceptional_victim_refused_cooperate = NA
)
for (file in administrative_files) {
administrative_temp <- readRDS(file)
hour_fix <- c(
"^on or between midnight and 00:59$" = "0",
"^on or between 01:00 and 01:59$" = "1",
"^on or between 02:00 and 02:59$" = "2",
"^on or between 03:00 and 03:59$" = "3",
"^on or between 04:00 and 04:59$" = "4",
"^on or between 05:00 and 05:59$" = "5",
"^on or between 06:00 and 06:59$" = "6",
"^on or between 07:00 and 07:59$" = "7",
"^on or between 08:00 and 08:59$" = "8",
"^on or between 09:00 and 09:59$" = "9",
"^on or between 10:00 and 10:59$" = "10",
"^on or between 11:00 and 11:59$" = "11",
"^on or between 12:00 and 12:59$" = "12",
"^on or between 13:00 and 13:59$" = "13",
"^on or between 14:00 and 14:59$" = "14",
"^on or between 15:00 and 15:59$" = "15",
"^on or between 16:00 and 16:59$" = "16",
"^on or between 17:00 and 17:59$" = "17",
"^on or between 18:00 and 18:59$" = "18",
"^on or between 19:00 and 19:59$" = "19",
"^on or between 20:00 and 20:59$" = "20",
"^on or between 21:00 and 21:59$" = "21",
"^on or between 22:00 and 22:59$" = "22",
"^on or between 23:00 and 23:59$" = "23"
)
administrative_temp$incident_date_hour[administrative_temp$incident_date_hour %in% c("1-", "2-", "unknown")] <- NA
administrative_temp$hour <- administrative_temp$incident_date_hour
administrative_temp$hour <- stringr::str_replace_all(administrative_temp$hour, hour_fix)
administrative_temp$hour <- as.numeric(administrative_temp$hour)
administrative_final$number_of_agencies[administrative_final$year %in% unique(administrative_temp$year)] <- length(unique(administrative_temp$ori))
# Exceptionally cleared subcategories
administrative_temp_exceptional <-
administrative_temp %>%
filter(!cleared_exceptionally %in% "not applicable",
!is.na(cleared_exceptionally))
administrative_final$percent_exceptional_offender_death[administrative_final$year %in% unique(administrative_temp$year)] <-
mean(administrative_temp_exceptional$cleared_exceptionally %in% "death of offender")
administrative_final$percent_exceptional_extradition_denied[administrative_final$year %in% unique(administrative_temp$year)] <-
mean(administrative_temp_exceptional$cleared_exceptionally %in% "extradition denied")
administrative_final$percent_exceptional_juvenile_no_custody[administrative_final$year %in% unique(administrative_temp$year)] <-
mean(administrative_temp_exceptional$cleared_exceptionally %in% "juvenile/no custody (the handling of a juvenile without taking him/her into custody, but rather by oral or written notice given to the parents or legal guardian in a case involving a minor offense, such as a petty larceny)")
administrative_final$percent_exceptional_prosecution_declined[administrative_final$year %in% unique(administrative_temp$year)] <-
mean(administrative_temp_exceptional$cleared_exceptionally %in% "prosecution declined (by the prosecutor for other than lack of probable cause)")
administrative_final$percent_exceptional_victim_refused_cooperate[administrative_final$year %in% unique(administrative_temp$year)] <-
mean(administrative_temp_exceptional$cleared_exceptionally %in% "victim refused to cooperate (in the prosecution)")
# Exceptionally cleared
administrative_final$percent_cleared_exceptionally[administrative_final$year %in% unique(administrative_temp$year)] <-
mean(!administrative_temp$cleared_exceptionally %in% "not applicable")
# With arrest
administrative_final$percent_with_arrest[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$total_arrestee_segments > 0)
administrative_final$percent_with_arrest_or_cleared_exceptionally[administrative_final$year %in% unique(administrative_temp$year)] <-
mean(administrative_temp$total_arrestee_segments > 0 |
!administrative_temp$cleared_exceptionally %in% "not applicable")
# Offense segments
administrative_final$mean_number_offense_segments[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$total_offense_segments)
administrative_final$median_number_offense_segments[administrative_final$year %in% unique(administrative_temp$year)] <- median(administrative_temp$total_offense_segments)
# Victim segments
administrative_final$mean_number_victim_segments[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$total_victim_segments)
administrative_final$median_number_victim_segments[administrative_final$year %in% unique(administrative_temp$year)] <- median(administrative_temp$total_victim_segments)
# Offender segments
administrative_final$mean_number_offender_segments[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$total_offender_segments)
administrative_final$median_number_offender_segments[administrative_final$year %in% unique(administrative_temp$year)] <- median(administrative_temp$total_offender_segments)
# Arrestee segments
administrative_final$mean_number_arrestee_segments[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$total_arrestee_segments)
administrative_final$median_number_arrestee_segments[administrative_final$year %in% unique(administrative_temp$year)] <- median(administrative_temp$total_arrestee_segments)
# Hours
ordered_hours <- sort(table(administrative_temp$incident_date_hour))
ordered_hours_excluding_midnight <- sort(table(administrative_temp$incident_date_hour[!administrative_temp$incident_date_hour %in%
"on or between midnight and 00:59"]))
ordered_hours_excluding_midnight_and_noon <-
sort(table(administrative_temp$incident_date_hour[!administrative_temp$incident_date_hour %in%
c(
"on or between midnight and 00:59",
"on or between 12:00 and 12:59"
)]))
administrative_final$mean_hour[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$hour, na.rm = TRUE)
administrative_final$mean_hour_excluding_midnight[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$hour[!administrative_temp$hour %in% 0],
na.rm = TRUE
)
administrative_final$most_common_hour[administrative_final$year %in% unique(administrative_temp$year)] <- names(ordered_hours[length(ordered_hours)])
administrative_final$most_common_hour_excluding_midnight[administrative_final$year %in% unique(administrative_temp$year)] <-
names(ordered_hours_excluding_midnight[length(ordered_hours_excluding_midnight)])
administrative_final$most_common_hour_excluding_midnight_and_noon[administrative_final$year %in% unique(administrative_temp$year)] <-
names(ordered_hours_excluding_midnight_and_noon[length(ordered_hours_excluding_midnight_and_noon)])
administrative_final$least_common_hour[administrative_final$year %in% unique(administrative_temp$year)] <- names(ordered_hours[1])
administrative_final$percent_hour_midnight[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$hour %in% 0)
administrative_final$percent_hour_noon[administrative_final$year %in% unique(administrative_temp$year)] <- mean(administrative_temp$hour %in% 12)
administrative_final$percent_hour_unknown[administrative_final$year %in% unique(administrative_temp$year)] <- mean(is.na(administrative_temp$hour))
message(file)
}
administrative_final
saveRDS(administrative_final, "data/nibrs_summary_stats/nibrs_administrative_summary_stats.rds")
# Arrestee ----------------------------------------------------------------
arrestee_files <- list.files("F:/ucr_data_storage/clean_data/NIBRS",
pattern = "nibrs_arrestee.*rds$",
full.names = TRUE
)
arrestee_files
group_b_arrestee_files <- list.files("F:/ucr_data_storage/clean_data/NIBRS",
pattern = "nibrs_group_b_arrest.*rds$",
full.names = TRUE
)
group_b_arrestee_files
arrestee_offense_first_year <- data.frame()
arrestee_final <- data.frame(
year = 1991:2023,
number_of_agencies = NA,
percent_unknown_age = NA,
median_age = NA,
mean_age = NA,
percent_male = NA,
percent_female = NA,
percent_unknown_race = NA,
percent_asian = NA,
percent_black = NA,
percent_american_indian = NA,
percent_white = NA,
percent_native_hawaiian = NA,
percent_hispanic = NA,
percent_not_hispanic = NA,
percent_ethnicity_unknown = NA,
percent_resident_status_resident = NA,
percent_resident_status_nonresident = NA,
percent_resident_status_unknown = NA,
percent_murders_gun = NA,
percent_murders_handgun = NA,
percent_murders_unarmed = NA,
percent_murders_knife = NA,
percent_murders_club_blackjack = NA,
percent_arrest_type_on_view = NA,
percent_arrest_type_summoned_cited = NA,
percent_arrest_type_taken_into_custody = NA,
percent_murder_arrest_type_on_view = NA,
percent_murder_arrest_type_summoned_cited = NA,
percent_murder_arrest_type_taken_into_custody = NA,
percent_dui_arrest_type_on_view = NA,
percent_dui_arrest_type_summoned_cited = NA,
percent_dui_arrest_type_taken_into_custody = NA,
percent_referred_to_other_authorities = NA,
percent_handled_within_department = NA,
percent_rifle_automatic = NA,
percent_shotgun_automatic = NA,
percent_other_firearm_automatic = NA,
percent_firearm_type_not_stated_automatic = NA,
percent_handgun_automatic = NA
)
for (file in arrestee_files) {
arrestee_temp <- readRDS(file)
arrestee_temp_old <- arrestee_temp
group_b_arrestee_temp <- readRDS(group_b_arrestee_files[grep(parse_number(file), group_b_arrestee_files)]) %>%
mutate(
automatic_weapon_indicator_1 = as.character(automatic_weapon_indicator_1),
automatic_weapon_indicator_2 = as.character(automatic_weapon_indicator_2),
arrestee_weapon_2 = as.character(arrestee_weapon_2)
)
arrestee_temp <-
arrestee_temp %>%
mutate(automatic_weapon_indicator_2 = as.character(automatic_weapon_indicator_2)) %>%
bind_rows(group_b_arrestee_temp)
arrestee_temp$age_of_arrestee[arrestee_temp$age_of_arrestee %in% "unknown"] <- NA
arrestee_temp$age_of_arrestee[arrestee_temp$age_of_arrestee %in% "over 98 years old"] <- 99
arrestee_temp$age_of_arrestee <- as.numeric(arrestee_temp$age_of_arrestee)
arrestee_temp$sex_of_arrestee[is.na(arrestee_temp$sex_of_arrestee)] <- "unknown"
arrestee_temp$race_of_arrestee[is.na(arrestee_temp$race_of_arrestee)] <- "unknown"
arrestee_temp$ethnicity_of_arrestee[is.na(arrestee_temp$ethnicity_of_arrestee)] <- "unknown"
arrestee_temp$resident_status_of_arrestee[is.na(arrestee_temp$resident_status_of_arrestee)] <- "unknown"
arrestee_final$number_of_agencies[arrestee_final$year %in% unique(arrestee_temp$year)] <- length(unique(arrestee_temp$ori))
# Automatic
arrestee_final$percent_rifle_automatic[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$automatic_weapon_indicator_1[arrestee_temp$arrestee_weapon_1 %in% "rifle"] %in% "automatic weapon")
arrestee_final$percent_shotgun_automatic[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$automatic_weapon_indicator_1[arrestee_temp$arrestee_weapon_1 %in% "shotgun"] %in% "automatic weapon")
arrestee_final$percent_other_firearm_automatic[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$automatic_weapon_indicator_1[arrestee_temp$arrestee_weapon_1 %in% "other firearm"] %in% "automatic weapon")
arrestee_final$percent_firearm_type_not_stated_automatic[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$automatic_weapon_indicator_1[arrestee_temp$arrestee_weapon_1 %in% "firearm (type not stated)"] %in% "automatic weapon")
arrestee_final$percent_handgun_automatic[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$automatic_weapon_indicator_1[arrestee_temp$arrestee_weapon_1 %in% "handgun"] %in% "automatic weapon")
# Disposition
arrestee_temp_juvenile <-
arrestee_temp %>%
filter(age_of_arrestee < 18)
arrestee_final$percent_referred_to_other_authorities[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp_juvenile$disposition_of_arrestee_under18 %in% "referred to other authorities (turned over to juvenile court, probation department, welfare agency, other police agency, criminal or adult court, etc.)")
arrestee_final$percent_handled_within_department[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp_juvenile$disposition_of_arrestee_under18 %in% "handled within department (released to parents, released with warning, etc.)")
# Age
arrestee_final$median_age[arrestee_final$year %in% unique(arrestee_temp$year)] <- median(arrestee_temp$age_of_arrestee, na.rm = TRUE)
arrestee_final$mean_age[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$age_of_arrestee, na.rm = TRUE)
arrestee_final$percent_unknown_age[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(is.na(arrestee_temp$age_of_arrestee))
# Sex
arrestee_final$percent_male[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$sex_of_arrestee %in% "male")
arrestee_final$percent_female[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$sex_of_arrestee %in% "female")
# Race
arrestee_final$percent_unknown_race[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$race_of_arrestee %in% "unknown")
arrestee_final$percent_asian[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$race_of_arrestee %in% "asian")
arrestee_final$percent_black[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$race_of_arrestee %in% "black")
arrestee_final$percent_american_indian[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$race_of_arrestee %in% "american indian/alaskan native")
arrestee_final$percent_white[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$race_of_arrestee %in% "white")
arrestee_final$percent_native_hawaiian[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$race_of_arrestee %in% "native hawaiian or other pacific islander")
# Ethnicity
arrestee_final$percent_hispanic[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$ethnicity_of_arrestee %in% "hispanic or latino")
arrestee_final$percent_not_hispanic[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$ethnicity_of_arrestee %in% "not hispanic or latino")
arrestee_final$percent_ethnicity_unknown[arrestee_final$year %in% unique(arrestee_temp$year)] <- mean(arrestee_temp$ethnicity_of_arrestee %in% "unknown")
# Resident status
arrestee_final$percent_resident_status_resident[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$resident_status_of_arrestee %in% "resident")
arrestee_final$percent_resident_status_nonresident[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$resident_status_of_arrestee %in% "nonresident")
arrestee_final$percent_resident_status_unknown[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$resident_status_of_arrestee %in% "unknown")
# Weapon
arrestee_final$percent_murders_gun[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$arrestee_weapon_1[arrestee_temp$ucr_arrest_offense_code %in% "murder/nonnegligent manslaughter"] %in%
c(
"rifle",
"handgun",
"shotgun",
"firearm (type not stated)",
"other firearm"
))
arrestee_final$percent_murders_handgun[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$arrestee_weapon_1[arrestee_temp$ucr_arrest_offense_code %in% "murder/nonnegligent manslaughter"] %in%
"handgun")
arrestee_final$percent_murders_unarmed[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$arrestee_weapon_1[arrestee_temp$ucr_arrest_offense_code %in% "murder/nonnegligent manslaughter"] %in%
"unarmed")
arrestee_final$percent_murders_knife[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$arrestee_weapon_1[arrestee_temp$ucr_arrest_offense_code %in% "murder/nonnegligent manslaughter"] %in%
"lethal cutting instrument (knife, ice pick, screwdriver, ax, etc.)")
arrestee_final$percent_murders_club_blackjack[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$arrestee_weapon_1[arrestee_temp$ucr_arrest_offense_code %in% "murder/nonnegligent manslaughter"] %in%
"blunt object (e.g., club/blackjack/brass knuckles)")
# Arrest type
arrestee_final$percent_arrest_type_on_view[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$type_of_arrest %in%
"on-view arrest (taken into custody without a warrant or previous incident report)")
arrestee_final$percent_arrest_type_summoned_cited[arrestee_final$year %in% unique(arrestee_temp$year)] <-
mean(arrestee_temp$type_of_arrest %in%
"summoned/cited (not taken into custody)")
arrestee_final$percent_arrest_type_taken_into_custody[arrestee_final$year %in% unique(arrestee_temp$year)] <-