---
title: "Analyzing the Local Law 97 of 2021-Pets in Shelter Report"
output: rmarkdown::html_vignette
author: "Xinru Wang"
vignette: >
  %\VignetteIndexEntry{Analyzing the Local Law 97 of 2021-Pets in Shelter Report}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup, include = FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(nycOpenData)
library(ggplot2)
library(dplyr)
```

## Introduction

New York City serves a large and diverse population, including individuals and families who experience housing instability and may rely on homeless shelters for temporary support. Pets serve many important roles in people’s lives, and understanding pet ownership among shelter entrants helps inform shelter policy, access to services, and resource planning.

The **Local Law 97 of 2021 – Pets in Shelter Report** provides quarterly survey data on the types of pets owned by people entering New York City homeless shelters. This dataset was recently added to NYC Open Data and made publicly available on October 14, 2025. These data are published through NYC Open Data and are available [here](https://data.cityofnewyork.us/Social-Services/Local-Law-97-of-2021-Pets-in-Shelter-Report/5nux-zfmw/about_data).

The `nycOpenData` package provides a streamlined interface for accessing New York City’s open data resources directly in R. By using the `5nux-zfmw()` function, we can retrieve the most recent quarterly shelter pet data and explore trends in pet ownership across time using familiar data analysis tools.

## Pulling a Small Sample

To start, we’ll pull a small sample of the data to get a sense of what it looks like. By default, the function retrieves the 10,000 most recent requests, so we’ll adjust this and only load the latest 3 by using `limit = 3`.

```{r small-sample}
small_sample <- nyc_pull_dataset("5nux-zfmw", limit = 3)

# Seeing what columns are in the dataset
names(small_sample)
```

Fantastic! We successfully pulled the `Local Law 97 of 2021-Pets in Shelter Report` data from the NYC Open Data Portal using the `5nux-zfmw()` function.

Let’s now look at an example using the most recent 3 quarters of data. The `5nux-zfmw()` function allows users to filter based on any of the columns in the dataset. To apply filters, we add `filters = list()` and specify the desired conditions. From our earlier inspection of the column names, we know that there are columns such as `date_year` and `date_quarter` that can be used to filter the data by time period.

```{r filter-year}

recent_quarters <- nyc_pull_dataset(
  "5nux-zfmw", 
  limit = 3,
  filters = list(date_year = 2024))

recent_quarters

# Checking to see the filtering worked
recent_quarters |>
  distinct(date_year)
```

Success! From calling the filtered dataset, we see that only three rows of data are returned, and the `distinct()` check confirms that all observations come from the specified year.

One of the strongest qualities of this function is its ability to filter based on multiple columns at once. Let’s put everything together and create a dataset containing all available quarterly records for a selected year where shelter entrants reported having pets.

```{r filter-multiple}
# Creating the dataset
pets_filtered <- nyc_pull_dataset(
  "5nux-zfmw",
  limit = 20,
  filters = list(
    date_year = "2024",
    number_of_birds = 0))

# Calling head of our new dataset
pets_filtered |>
  slice_head(n = 2)

# Quick check to make sure our filtering worked
pets_filtered |>
  summarize(rows = n())

pets_filtered |>
  distinct(date_year)

pets_filtered |>
  distinct(number_of_birds)
```

We successfully created a dataset containing the most recent quarterly records for a selected year in which shelter entrants reported having no birds.

## Mini analysis

Now that we’ve successfully pulled the data and have it in R, let’s do a quick mini analysis to look at how the number of shelter entrants who reported having pets changes over time.

To do this, we’ll create a simple bar chart showing the number of applicants with pets in each year.

```{r had-pet-year-graph, fig.alt="Bar chart showing the number of shelter applicants with pets by year.", fig.cap="Bar chart showing the number of shelter applicants with pets by year.", fig.height=5, fig.width=7}

pets <- nyc_pull_dataset("5nux-zfmw", limit = 100)
pets <- pets |>
  mutate(had_pet = as.numeric(had_pet))

# summarize by year
pet_by_year <- pets |>
  group_by(date_year) |>
  summarize(applicants_with_pets = sum(had_pet, na.rm = TRUE))

# Plot
ggplot(pet_by_year, aes(x = date_year, y = applicants_with_pets)) +
  geom_col(fill = "darkseagreen") +
  theme_minimal() +
  labs(
    title = "Shelter Applicants With Pets by Year (NYC)",
    subtitle = "Local Law 97 - Pets in Shelter Report",
    x = "Year",
    y = "Number of Applicants With Pets"
  )
```

This graph shows how the number of shelter entrants reporting pets changes across years, which can help highlight broader trends and guide planning for pet-related shelter resources.

## Summary
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.

As demonstrated in this vignette, the package provides a seamless workflow for targeted data retrieval, automated filtering, and rapid visualization.

## How to Cite
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/.

