knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
knitr::opts_chunk$set(echo = TRUE) library(nycOpenData) library(ggplot2) library(dplyr)
This dataset contains information about the kind of violent incident that happens among inmates in prison. Each row provides the incident ID, the date the incident was reported, the incident type, and the facility where it occurred. A researcher might want to use this dataset to investigate the kind of crimes inmates commit while they are incarcerated.
The nycOpenData package provides a streamlined interface for accessing New York City's vast open data resources. It connects directly to the NYC Open Data Portal. It is currently utilized as a primary tool for teaching data acquisition in Reproducible Research Using R, helping students bridge the gap between raw city APIs and tidy data analysis.
By using the nyc_slash_stab function, we can investigate the most recent violent incidents that occurred among incarcerated individuals to understand their nature and fatality.
To start, let's pull a small sample to see what the data looks like. By default, the function pulls in the 10,000 most recent requests, however, let's change that to only see the latest 3 requests. To do this, we can set limit = 3.
small_sample <- nyc_slash_stab(limit = 100) small_sample # Seeing what columns are in the dataset colnames(small_sample) unique(small_sample$facility)
The nyc_slash_stab() function can filter based off any of the columns in the dataset. To filter, we add filters = list() and put whatever filters we would like inside. From our colnames call before, we know that there is a column called "incident_type" which we can use to accomplish this.
incident_slash_stab <- nyc_slash_stab(limit = 3, filters = list(incident_type = "Stabbing")) incident_slash_stab # Checking to see the filtering worked unique(incident_slash_stab$incident_type)
The previous sections were riddled with errors in my console! The next step would be to investigate the "facility" column and potentially run correlational analysis and plot the results between "incident_type" and "facility".
This section was meant to look into the most recurring incident types among inmates and take note of their severity. But for future inquiries:
# Creating the datasets slash <- nyc_slash_stab(limit = 50, filters = list(facility = "AMKC", incident_type = "Slashing")) stab <- nyc_slash_stab(limit = 50, filters = list(facility = "AMKC", incident_type = "Stabbing")) # Calling head of our new dataset head(slash) head(stab) # Quick check to make sure our filtering worked nrow(slash) nrow((stab)) unique(slash$facility) unique(stab$facility)
This code should allow us to see how slashing and stabbing incidents vary by facilities.
As an example of how this dataset can be used for exploratory analysis, the code below groups incidents by facility and incident type, then visualizes the resulting counts. This approach offers a straightforward way to compare patterns of violence across locations.
data <- nyc_slash_stab(limit = 100) %>% filter(incident_type %in% c("Slashing", "Stabbing")) %>% count(facility, incident_type, name = "count") ggplot(data, aes(x = incident_type, y = count, fill = facility)) + geom_col(position = "dodge") + theme_minimal() + labs( title = "Slashing vs Stabbing Incidents by Facility", x = "Incident Type", y = "Number of Incidents", fill = "Facility" )
The nycOpenData package serves as a robust interface for the NYC Open Data portal, streamlining the path from raw city APIs to actionable insights. By abstracting the complexities of data acquisition—such as pagination, type-casting, and complex filtering—it allows users to focus on analysis rather than data engineering.
If certain codes had worked as intneded in this vignette, one could see how the package provides a seamless workflow for targeted data retrieval, automated filtering, and rapid visualization. Importantly, It's worth noting that some sections need to be improved as soon as possible.
If you use this package for research or educational purposes, please cite it as follows:
Martinez C (2026). nycOpenData: Convenient Access to NYC Open Data API Endpoints. R package version 0.1.6, https://martinezc1.github.io/nycOpenData/.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.