An R script that calculates the total cost of crime for neighborhoods using the RAND Corporation's cost of crime values
See a visualization of the R script's output in a web map here, which was built using Mapbox GL JS and Mapbox Studio! You can see the code in the index.html file in this repo.
_This shows the crime type with the greatest cost for each neighborhood in 2015_
What does this R script do? This R script counts the number of crimes in each Philadelphia neighborhood and calculates the cost of each type of crime. Values for the total cost of all crimes per neighborhood, as well as which crime type has the highest cost per neighborhood, are also calculated.
The script outputs two shapefiles, one for neighborhoods and the other for Census block groups.
I've included a QGIS project file with styling (shown above) that highlights the crime type that represents the greatest cost for that polygon.
How can I run this myself? These instructions assume you have R installed:
- Clone the repo
- Run the script from the script's directory with
Rscript cost_of_crime.R
- Check the
output
directory for the shapefiles created from the script
Related notes:
The script requires the
input
directory
The
output
directory is included for reference
This script is configured to overwrite the contents of
output
What are next steps? I plan to make it easier to plug in a new city's open crime data and neighborhood shapefile to make this portable to other municipalities.
I also plan to introduce a web-based map that makes it easy to explore this data in the browser. Done! Check it out here or dig into the code in this repo in the index.html file.
Special Thanks I'm using the the City of Philadelphia's open crime data, the RAND Corporation's cost of crime calculator values, and Azavea's excellent Philadelphia neighborhoods shapefile.