generated from KSUDS/p2_data
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_functions.R
152 lines (134 loc) · 5.38 KB
/
_functions.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
library(readr)
library(dplyr)
library(tibble)
library(magrittr)
# library(arrow)
# Function for downloading and parsing data:
parse_cdc <- function(year, url, folder_path = "death_data") {
#last2 year
l2 <- stringr::str_sub(year, 3, 4)
fnames <- c("deaths", "guns", "suicide")
#tibble of names
tnames <- tibble::tibble(
folder = folder_path,
start_name = fnames,
year = l2,
name = stringr::str_c(start_name, year, sep = "_"),
file_path_feather = file.path(folder, stringr::str_c(name, ".feather")),
file_path_rds = file.path(folder, stringr::str_c(name, ".rds"))
)
# First download data. These are fixed-width files.
# Layout for recent years (need tweaks for earlier year)
layout <- readr::fwf_widths(
widths = c(19, 1, 40, 2, 1, 1, 2, 2, 1, 4,
1, 2, 2, 2, 2, 1, 1, 1, 16, 4, 1, 1, 1,
1, 34, 1, 1, 4, 3, 1, 3, 3, 2, 1, 281,
1, 2, 1, 1, 1, 1, 33, 3, 1, 1),
col_names = c("drop1", "res_status", "drop2", "education_89",
"education_03", "education_flag", "month", "drop3", "sex",
"detail_age", "age_flag", "age_recode", "age_recode2", "age_group",
"age_infant", "death_place", "marital", "day_of_week", "drop4", "data_year",
"at_work", "death_manner", "burial", "autopsy", "drop5", "activity",
"injury_place", "underlying_cause", "cause_recode358", "drop6",
"cause_recode113", "cause_recode130", "cause_recode39", "drop7",
"multiple_causes", "drop8", "race", "race_bridged", "race_flag",
"race_recode", "race_recode2", "drop9", "hispanic", "drop10",
"hispanic_recode")
)
# files are large.
options(timeout = 120) # raise to two minutes
temp <- tempfile()
utils::download.file(url, temp, quiet = T)
options(timeout = 60) # back to default
# Read in data
raw_file <- readr::read_fwf(unzip(temp), layout)
# Drop empty fields
raw_file <- raw_file %>%
dplyr::select(-contains("drop"))
# Subset suicides
# Suicide codes: X60 - X84, U03, Y870
suicide_code <- c(stringr::str_c("X", 60:84), "U03", "Y870")
# Gun suicides
# X72 (Intentional self-harm by handgun discharge)
# X73 (Intentional self-harm by rifle, shotgun and larger firearm discharge)
# X74 (Intentional self-harm by other and unspecified firearm discharge)
suicide <- raw_file %>%
dplyr::filter(underlying_cause %in% suicide_code) %>%
dplyr::mutate(
gun = ifelse(underlying_cause %in% c("X72", "X73", "X74"), 1, 0),
year = year
)
# Subset firearm deaths
# Firearm death codes
# Accidental:
# W32 (Handgun discharge)
# W33 (Rifle, shotgun and larger firearm discharge)
# W34 (Discharge from other and unspecified firearms)
#
# Suicide:
# X72 (Intentional self-harm by handgun discharge)
# X73 (Intentional self-harm by rifle, shotgun and larger firearm discharge)
# X74 (Intentional self-harm by other and unspecified firearm discharge)
#
# Homicide:
# U01.4 (Terrorism involving firearms)
# X93 (Assault by handgun discharge)
# X94 (Assault by rifle, shotgun and larger firearm discharge)
# X95 (Assault by other and unspecified firearm discharge)
#
# Undetermined intent:
# Y22 (Handgun discharge, undetermined intent)
# Y23 (Rifle, shotgun and larger firearm discharge, undetermined intent)
# Y24 (Other and unspecified firearm discharge, undetermined intent)
#
# Legal intervention
# (Note that we code legal intervention deaths as homicides)
# Y35.0 (Legal intervention involving firearm discharge)
# Add categorical variable for intent, weapon, plus dummy for police shootings
guns <- raw_file %>%
dplyr::filter(underlying_cause %in%
c("W32", "W33", "W34", "X72", "X73", "X74", "U014",
"X93", "X94", "X95", "Y22", "Y23", "Y24", "Y350")) %>%
dplyr::mutate(
intent = dplyr::case_when(
underlying_cause %in% c("W32", "W33", "W34") ~ "Accidental",
underlying_cause %in% c("X72", "X73", "X74") ~ "Suicide",
underlying_cause %in% c("*U01.4", "X93", "X94", "X95",
"Y350") ~ "Homicide",
underlying_cause %in% c("Y22", "Y23", "Y24") ~ "Undetermined",
TRUE ~ NA_character_),
police = ifelse(underlying_cause == "Y350", 1, 0),
weapon = dplyr::case_when(
underlying_cause %in% c("W32", "X72", "X93", "Y22") ~ "Handgun",
underlying_cause %in% c("W33", "X73", "X94", "Y23") ~ "Rifle etc",
TRUE ~ "Other/unknown"),
# Create a cleaner age variable. Every age under 1 year will "0"
age = dplyr::case_when(
stringr::str_sub(detail_age, 1, 1) == "1" ~
as.numeric(stringr::str_sub(detail_age, 2, 4)),
detail_age == 9999 ~ NA_real_,
TRUE ~ 0),
age = ifelse(age == 999, NA, age),
year = year)
## save files
# create folder
dir.create(folder_path)
# suicide
readr::write_rds(suicide,
tnames %>%
dplyr::filter(start_name == "suicide") %>%
dplyr::pull(file_path_rds))
# guns
readr::write_rds(guns,
tnames %>%
dplyr::filter(start_name == "guns") %>%
dplyr::pull(file_path_rds))
# Save 'all_deaths' file
readr::write_rds(raw_file,
tnames %>%
dplyr::filter(start_name == "deaths") %>%
dplyr::pull(file_path_rds))
print(stringr::str_c("make sure to add ",
folder_path,
" to your .gitignore if you are using git"))
}