library(tidyverse)
library(here)
library(skimr)
library(janitor)
library(readr)
crime <- read_csv(here("data", "buffalocrime.csv"))
The following code was used to clean the raw crime data set and excluded all information except the incident case number, date and time, crime category, and neighborhood where the crime took place.
cleancrime <- crime %>%
clean_names() %>%
rename(incident_category = parent_incident_type) %>%
select(case_number, incident_datetime, incident_category, neighborhood)
The following code was used to count the number of criminal incidents which occurred in each neighborhood and sorted the data by the number of crimes.
cleancrime %>%
arrange(neighborhood, incident_category) %>%
group_by(neighborhood) %>%
count(incident_category) %>%
rename(crime_count = n) %>%
arrange(-crime_count)
## # A tibble: 332 × 3
## # Groups: neighborhood [37]
## neighborhood incident_category crime_count
## <chr> <chr> <int>
## 1 North Park Theft 9658
## 2 Central Theft 8891
## 3 Elmwood Bidwell Theft 6714
## 4 Elmwood Bryant Theft 6563
## 5 Kensington-Bailey Theft 6381
## 6 Broadway Fillmore Theft 5650
## 7 Upper West Side Theft 4524
## 8 University Heights Theft 4443
## 9 West Side Theft 4120
## 10 Broadway Fillmore Assault 4052
## # ℹ 322 more rows
The following code was used to count the number of homicides which occurred in each neighborhood and sorted the data by the number of homicides.
cleancrime %>%
filter(incident_category == "Homicide") %>%
group_by(neighborhood) %>%
count(incident_category) %>%
rename(homicide_count = n) %>%
arrange(-homicide_count)
## # A tibble: 37 × 3
## # Groups: neighborhood [37]
## neighborhood incident_category homicide_count
## <chr> <chr> <int>
## 1 Broadway Fillmore Homicide 113
## 2 Genesee-Moselle Homicide 103
## 3 Kensington-Bailey Homicide 66
## 4 Schiller Park Homicide 63
## 5 Delavan Grider Homicide 53
## 6 Masten Park Homicide 50
## 7 Kenfield Homicide 45
## 8 Fillmore-Leroy Homicide 43
## 9 MLK Park Homicide 40
## 10 West Side Homicide 37
## # ℹ 27 more rows