fatal <- read.csv("FATAL ENCOUNTERS DOT ORG SPREADSHEET (See Read me tab) - Form Responses.csv")
acs_state <- tidycensus::get_acs(geography = "state",
variables = c(hhincome = 'B19013_001'),
year = 2023,
output = "wide",
geometry = TRUE) %>%
#drop AK, HI, PR leaving CONUS as study area
filter(!(NAME %in% c("Alaska", "Hawaii", "Puerto Rico")))
## | | | 0% | | | 1% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 17% | |============ | 18% | |============= | 18% | |============= | 19% | |============== | 20% | |============== | 21% | |=============== | 21% | |=============== | 22% | |================ | 23% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |===================== | 31% | |====================== | 31% | |====================== | 32% | |======================= | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 40% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 49% | |=================================== | 50% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 57% | |========================================= | 58% | |========================================= | 59% | |========================================== | 59% | |========================================== | 60% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 62% | |============================================ | 63% | |============================================= | 64% | |============================================= | 65% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 66% | |=============================================== | 67% | |================================================ | 68% | |================================================ | 69% | |================================================= | 69% | |================================================= | 70% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 73% | |=================================================== | 74% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 83% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 90% | |=============================================================== | 91% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
fatal_gunshot <- fatal %>%
#filter to only deaths by gunshot, drop AK, HI, PR leaving CONUS as study area
filter(Highest.level.of.force == "Gunshot",
!(State %in% c("AK", "HI", "PR"))) %>%
mutate(
#add "region" column to fatal df
Region = case_when(
State %in% c("ME", "NH", "VT", "MA", "RI", "CT", "NY", "NJ", "PA") ~ "Northeast",
State %in% c("OH", "IN", "IL", "MI", "WI",
"MN", "IA", "MO", "ND", "SD", "NE", "KS") ~ "Midwest",
State %in% c("DE", "MD", "DC", "VA", "WV", "NC", "SC", "GA", "FL",
"KY", "TN", "AL", "MS", "AR", "LA", "OK", "TX") ~ "South",
State %in% c("MT", "ID", "WY", "CO", "NM", "AZ", "UT", "NV") ~ "Mountain West",
State %in% c("WA", "OR", "CA") ~ "Pacific",
TRUE ~ "Other"
),
#extract year to separate column
Year = str_sub(Date.of.injury.resulting.in.death..month.day.year., -4, -1),
#add column n for count
n=1) %>%
#select only columns in need
select(age=Age,
gender=Gender,
race=Race,
date=Date.of.injury.resulting.in.death..month.day.year.,
year=Year,
state=State,
region=Region,
lon=Longitude,
lat=Latitude,
n=n)
#check if all columns have been assigned region
if (!("Other" %in% fatal_gunshot$Region)){
print("All columns have been assigned to a region.")
}
## [1] "All columns have been assigned to a region."
acs_state <- acs_state %>%
#add state abbreviation column to acs_state df
mutate(state_abb = case_when(
NAME == "District of Columbia" ~ "DC",
TRUE ~ state.abb[match(NAME, state.name)]
)) %>%
#drop NA values in state abbreviation name
drop_na()
#summarize total gunshot death count by state
fatal_gunshot_summarize <- fatal_gunshot %>%
select(state, n) %>%
group_by(state) %>%
summarise(totaldeath = sum(n, na.rm = TRUE))
#join summarized fatal_gunshot to acs_state df
acs_state_count <- acs_state %>%
left_join(fatal_gunshot_summarize, by = c("state_abb" = "state")) %>%
select(name=NAME,
state_abb,
totaldeath)
#convert fata_gunshot df to sf object, match crs with acs_state_count
fatal_gunshot_sf <- st_as_sf(
fatal_gunshot,
coords = c("lon", "lat"), # your coordinate column names
crs = 4326 # EPSG:4326 = WGS84 (standard lat/lon)
)
acs_state_count <- st_transform(acs_state_count, st_crs(fatal_gunshot_sf))