For this assigmnent, we are going to use Leaflet function to map all the crime incidents that involved gun use in the City of Los Angeles in 2013.
Create a web page using R Markdown that features a map created with Leaflet.
Host your webpage on either GitHub Pages, RPubs, or NeoCities.
Your webpage must contain the date that you created the document, and it must contain a map created with Leaflet.
This dataset is obtained from DATA.GOV - The home of the U.S. Government’s open data. This dataset reflects incidents of crime in the City of Los Angeles dating back to 2010. This data is transcribed from original crime reports that are typed on paper and therefore there may be some inaccuracies within the data. Some location fields with missing data are noted as (0°, 0°). Address fields are only provided to the nearest hundred block in order to maintain privacy. This data is as accurate as the data in the database.
1- Libraries
library(dplyr)
library(knitr)
library(kableExtra)
library(leaflet)
2-Downloading the raw data.
# Setting working directory first
setwd("~/Coursera/8_Data_Science_Specialization/9 Developing Data Products/Week 2/Assignment")
# Downloading the .csv data file
if(!file.exists("Crime_Data_from_2010_to_Present.csv")) {
download.file("https://data.lacity.org/api/views/63jg-8b9z/rows.csv?accessType=DOWNLOAD",
destfile = "Crime_Data_from_2010_to_Present.csv",
method = "curl")}
# Loading the Data
A_1_Crime_Data <- read.csv("Crime_Data_from_2010_to_Present.csv")
3-Cleaning the data.
We are filtering all the crimes occurred only in 2013. To get the crimes that involved gun use, we are filtering the weapon’s description by all the words related with guns (excluding “Toy guns” and “Simulated guns”). And last, we are keeping only the important columns required for the Leaftlet function (Latitude and Longitude).
# Formating longitude and latitude variables
names(A_1_Crime_Data)[27]<-"lat" ; names(A_1_Crime_Data)[28]<-"lng"
# Filtering crimes ocurred only in 2013
A_1_Crime_Data$DATE.OCC <- as.Date(A_1_Crime_Data$DATE.OCC, format = "%m/%d/%Y")
A_1_Crime_Data <- A_1_Crime_Data %>% filter(DATE.OCC > as.Date("2012-12-31"))
# Filtering crimes committed with guns
A_2_Crime_Data <-
A_1_Crime_Data %>%
filter(grepl("gun|weapon|pistol|firearm|revolver|simulated gun|shotgun|rifle", Weapon.Desc,ignore.case = TRUE), !grepl("toy|simulated", Weapon.Desc, ignore.case = TRUE),
!is.na(Weapon.Used.Cd),
DR_NO != 152118665 # This is an excluded case with wrong lat and lng.
) %>%
select(DR_NO, lat, lng)
dim(A_2_Crime_Data)
## [1] 7017 3
As we can see, there are 7017 committed crimes. Now, let’s take a look at the most frequent crimes by weapon description.
Weapon.Used.Cd | Weapon.Desc | count |
---|---|---|
500 | UNKNOWN WEAPON/OTHER WEAPON | 3278 |
102 | HAND GUN | 1762 |
109 | SEMI-AUTOMATIC PISTOL | 778 |
106 | UNKNOWN FIREARM | 471 |
101 | REVOLVER | 368 |
114 | AIR PISTOL/REVOLVER/RIFLE/BB GUN | 101 |
Using the clustering tool to map all the crimes in the city, as you zoom-in an click the location of the crime (red spots) and click, the case number is displayed.
#Cluster
A_2_Crime_Data %>% leaflet() %>% addTiles() %>%
addMarkers(popup = paste("Case #:", A_2_Crime_Data$DR_NO,sep=" "),
clusterOptions=markerClusterOptions())