-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilterCode.Rmd
More file actions
184 lines (157 loc) · 5.87 KB
/
Copy pathfilterCode.Rmd
File metadata and controls
184 lines (157 loc) · 5.87 KB
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
---
title: "Project2Code"
author: "Eric Liang"
date: "12/10/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r message = F, warning = F}
library(tidyverse)
```
```{r}
orgDeaths <- read_csv("gun-violence-data.csv")
```
```{r}
gun_violence2018 <- subset(orgDeaths, substr(orgDeaths$date, 1, 4) == "2018")
gun_violence2018$state <- tolower(gun_violence2018$state)
gun_violence2018 <- mutate(gun_violence2018,
n_affected = as.factor(n_killed + n_injured))
gun_violence2018$n_affected <- fct_collapse(gun_violence2018$n_affected, "8+" = c("8", "16","34"))
```
```{r}
# filter out data to just one year
start_date <- as.Date("2017-03-01", format = "%Y-%m-%d")
end_date <- as.Date("2018-03-30", format = "%Y-%m-%d")
deaths <- mutate(orgDeaths, date = as.Date(date, format = "%Y-%m-%d")) %>%
filter(date >= start_date & date <= end_date)
```
```{r}
# Pre Cleaning for Gun Types
types <- deaths$gun_type
types <- unlist(lapply(types,function(x) str_split(x,"\\|\\|")[[1]][1]))
types <- unlist(lapply(types,function(x) str_split(x,"\\|")[[1]][1]))
types <- unlist(lapply(types,function(x) gsub("::",":",x)))
reduceCat <- function(x) {
if (grepl("gauge",x)){
return("Shotgun")
}
if (grepl("Win",x)){
return("Win")
}
if (grepl("Mag",x)){
return("Mag")
}
if (grepl("Auto",x)){
return("Auto")
}
if (grepl("10mm",x)){
return("Handgun")
}
if (grepl("9mm",x)){
return("Handgun")
}
if (grepl("Gauge",x)){
return("Shotgun")
}
if (grepl("30-06",x)){
return("Rifle")
}
if (grepl("223 Rem",x)){
return("AR-15")
}
if (grepl("38 Spl",x)){
return("Handgun")
}
if (grepl("40 SW",x)){
return("Handgun")
}
if (grepl("AK-47",x)){
return("AK-47")
}
return(x)
}
types <- unlist(lapply(types,reduceCat))
types <- unlist(lapply(types,function(x) gsub("0:","",x)))
deaths$gun_type <- types
```
```{r}
# filter out data to just PA
penn_deadly_data <- filter(deaths, state == "Pennsylvania"
& n_killed > 0 & !is.na(latitude))
```
```{r}
age_and_type <- select(deaths, participant_age, participant_type,
date, state) %>%
filter(!is.na(participant_age) & !is.na(participant_type)) %>%
mutate(ages = str_split(participant_age, "\\|\\|"),
types = str_split(participant_type, "\\|\\|"))
num_participants <- sapply(age_and_type$ages,function(x){return(length(x))})
age_and_type <- mutate(age_and_type, num_participants = num_participants) %>%
filter(num_participants > 1) %>% select(ages, types, num_participants, date,
state)
split_ages <- lapply(age_and_type$ages, function(x){return(substr(x, 4, 5))})
split_types <- lapply(age_and_type$types,
function(x){return(substr(x, 4, nchar(x)))})
age_and_type <- mutate(age_and_type, ages = split_ages, types = split_types)
num_victims <- sapply(age_and_type$types,
function(x){return(sum(x == "Victim"))})
num_suspects <- sapply(age_and_type$types,
function(x){return(sum(x == "Subject-Suspect"))})
age_and_type <- mutate(age_and_type, num_victims = num_victims,
num_suspects = num_suspects) %>%
filter(num_victims > 0 & num_suspects > 0)
victim_avg_age <- rep(0, nrow(age_and_type))
suspect_avg_ages <- rep(0, nrow(age_and_type))
for (i in 1:nrow(age_and_type)) {
victim_ages <- suppressWarnings(
as.numeric(unlist(age_and_type[i,]$ages)[which(
unlist(age_and_type[i,]$types) == "Victim")]))
suspect_ages <- suppressWarnings(as.numeric(
unlist(age_and_type[i,]$ages)[which(
unlist(age_and_type[i,]$types) == "Subject-Suspect" |
unlist(age_and_type[i,]$types) == ":Subject-Suspect")]))
victim_avg_age[i] <- mean(victim_ages, na.rm = TRUE)
suspect_avg_ages[i] <- mean(suspect_ages, na.rm = TRUE)
}
age_and_type <- mutate(age_and_type, avg_victim_age = victim_avg_age,
avg_suspect_age = suspect_avg_ages) %>%
select(date, state, avg_victim_age, avg_suspect_age) %>%
filter(!is.na(avg_victim_age) & !is.na(suspect_avg_ages))
get_region <- function(state) {
if (state %in% c("Maine", "New Hampshire", "Vermont", "Massachusetts",
"Rhode Island", "Connecticut", "New York", "Pennsylvania",
"New Jersey")) {
return("Northeast")
} else if (state %in% c("Wisconsin", "Michigan", "Illinois", "Indiana",
"Ohio", "North Dakota", "South Dakota", "Nebraska",
"Kansas", "Minnesota", "Iowa", "Missouri")) {
return("Midwest")
} else if (state %in% c("Delaware", "Maryland", "District of Columbia",
"Virginia", "West Virginia", "North Carolina",
"South Carolina", "Georgia", "Florida", "Kentucky",
"Tennessee", "Mississippi", "Alabama", "Oklahoma",
"Texas", "Arkansas", "Louisiana")) {
return("South")
} else {
return("West")
}
}
age_data_region <- mutate(age_and_type, region = sapply(state, get_region),
month = format(as.Date(date, format = "%Y-%m-%d"),
"%B %Y"))
```
```{r}
# write out the filtered data
deaths <- select(deaths,state,gun_type,notes,n_killed,n_injured,state)
write.csv(deaths,"filteredViolence.csv",row.names = FALSE)
penn_deaths <- select(penn_deadly_data,
n_killed,longitude,latitude,date,n_injured,source_url)
write.csv(penn_deaths,"pennDeadly.csv",row.names = FALSE)
write.csv(age_data_region,"gun-violence-ages.csv",row.names = FALSE)
injured_killed <- select(orgDeaths,date,n_injured,n_killed)
write.csv(injured_killed, "injured_killed.csv",row.names = FALSE)
violence_2018 <- select(gun_violence2018,n_guns_involved,n_affected)
write.csv(violence_2018,"violence_2018.csv",row.names = FALSE)
```