This dataset includes all valid felony, misdemeanor, and violation crimes reported to the New York City Police Department (NYPD) for all complete quarters so far this year (2017).
The original dataset provided by the New York City Police Department is in csv format.
dat <- read.csv("C:/Users/alexa/OneDrive/Desktop/STA 4233/NYC_crime.csv")
Here is the structure of the dataset:
str(dat)
## 'data.frame': 35 obs. of 24 variables:
## $ CMPLNT_NUM : int 749595529 134301299 813010521 314597003 253714310 193519890 540916634 514451869 870035835 370776797 ...
## $ ADDR_PCT_CD : int 34 25 13 30 20 32 17 13 33 28 ...
## $ BORO_NM : chr "MANHATTAN" "MANHATTAN" "MANHATTAN" "MANHATTAN" ...
## $ CMPLNT_FR_DT : chr "01/01/2016" "01/01/2016" "01/01/2016" "01/01/2016" ...
## $ CMPLNT_FR_TM : chr "00:01:00" "08:00:00" "00:01:00" "00:00:00" ...
## $ CMPLNT_TO_DT : chr "01/01/2018" "12/31/2017" "05/01/2019" "06/14/2021" ...
## $ CMPLNT_TO_TM : chr "00:01:00" "09:00:00" "23:59:00" "19:22:00" ...
## $ CRM_ATPT_CPTD_CD : chr "COMPLETED" "COMPLETED" "COMPLETED" "COMPLETED" ...
## $ HADEVELOPT : logi NA NA NA NA NA NA ...
## $ JURIS_DESC : chr "N.Y. POLICE DEPT" "N.Y. HOUSING POLICE" "N.Y. POLICE DEPT" "N.Y. POLICE DEPT" ...
## $ KY_CD : int 233 233 233 116 109 116 109 340 109 104 ...
## $ LAW_CAT_CD : chr "MISDEMEANOR" "MISDEMEANOR" "MISDEMEANOR" "FELONY" ...
## $ LOC_OF_OCCUR_DESC: chr "INSIDE" "INSIDE" "INSIDE" "INSIDE" ...
## $ OFNS_DESC : chr "SEX CRIMES" "SEX CRIMES" "SEX CRIMES" "SEX CRIMES" ...
## $ PARKS_NM : logi NA NA NA NA NA NA ...
## $ PD_CD : int 175 175 175 180 420 177 432 718 407 157 ...
## $ PD_DESC : chr "SEXUAL ABUSE 3,2" "SEXUAL ABUSE 3,2" "SEXUAL ABUSE 3,2" "COURSE OF SEXUAL CONDUCT AGAIN" ...
## $ PREM_TYP_DESC : chr "RESIDENCE - APT. HOUSE" "RESIDENCE - PUBLIC HOUSING" "PUBLIC SCHOOL" "RESIDENCE - APT. HOUSE" ...
## $ RPT_DT : chr "09/06/2021" "08/11/2021" "06/27/2021" "06/14/2021" ...
## $ X_COORD_CD : int 1002106 1000555 988874 999794 990909 999358 992408 988699 1000921 997376 ...
## $ Y_COORD_CD : int 249501 230994 207673 241252 224857 236472 214016 208577 242961 233927 ...
## $ Latitude : num 40.9 40.8 40.7 40.8 40.8 ...
## $ Longitude : num -73.9 -73.9 -74 -73.9 -74 ...
## $ Lat_Lon : chr "(40.85148777400008, -73.93545796699993)" "(40.800694331000045, -73.94110928599997)" "(40.736698070000045, -73.98331602599995)" "(40.82885104500008, -73.94383419099995)" ...
This dataset provides a series of useful information regarding the crime: its locations (longitude and latitude in degrees), the address (if available), the type of crime and the court outcome (if available). For this project, only the coordinates and the type of crime are used.
For some incidents the coordinates are not provided, and are removed from the data:
dat <- dat[!is.na(dat$Longitude)&!is.na(dat$Latitude),]
Since there are so many points on a map that it doesnโt make sense to plot every marker. So, plots of clusters of markers with addMarkers(clusterOptions = markerClusterOptions()) are used. You can zoom in to each cluster, the clusters will separate until you can see the individual markers. The Crime Type is added as a popup for each marker. Click on any blue marker to determine what crime was committed.
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.1.3
dat %>%
leaflet() %>%
addTiles() %>%
addMarkers(popup=dat$Crime.type , clusterOptions=markerClusterOptions())
## Assuming "Longitude" and "Latitude" are longitude and latitude, respectively
In this project open crime data was displayed using Leaflet, one of the most popular Javascript libraries for creating interactive maps. However, in only a few lines of code, the leaflet R package allowed the creation of my own leaflet map without needing to know any Javascript!