victimsPerDepartment.R (943B)
1 # This doesn't actually do what I want, but it's still useful code. Maybe? 2 library(readxl) 3 library(dplyr) 4 library(tidyr) 5 6 mpvData <- read_excel("MPVDatasetDownload-83zj.xlsx", sheet = 1) 7 8 # Pitch bad columns, which are empty and raise errors in dplyr 9 mpvData <- mpvData[, 1:19] 10 11 mpvData <- mpvData %>% 12 # Only data from 2015 and agency must be specified 13 filter(`Date of injury resulting in death (month/day/year)` >= 14 as.POSIXct("2015-01-01"), 15 !is.na(`Agency responsible for death`)) %>% 16 # Split up the agencies 17 mutate(agency = sub(",? and ", ", ", `Agency responsible for death`)) %>% 18 separate(agency, paste("agency", 1:4, sep="_"), 19 "[[:space:]]*,[[:space:]]*", fill = "right") %>% 20 # Gather back into a single column 21 gather("agency_number", "agency_name", starts_with("agency_"), na.rm = TRUE) 22 23 # Count the number of victims per department 24 victimsPerDepartment <- mpvData %>% 25 count(agency_name)