library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)

rats_raw <- read_csv("data/Rat_Sightings.csv", na = c("", "NA", "N/A"))
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 101914 Columns: 52
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (33): Created Date, Closed Date, Agency, Agency Name, Complaint Type, De...
## dbl  (6): Unique Key, Incident Zip, X Coordinate (State Plane), Y Coordinate...
## lgl (13): Landmark, Facility Type, School or Citywide Complaint, Vehicle Typ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
rats_clean <- rats_raw %>%
  rename(created_date = `Created Date`,
         location_type = `Location Type`,
         borough = Borough) %>%
  mutate(created_date = mdy_hms(created_date)) %>%
  mutate(sighting_year = year(created_date),
         sighting_month = month(created_date),
         sighting_day = day(created_date),
         sighting_weekday = wday(created_date, label = TRUE, abbr = FALSE)) %>%
  filter(borough != "Unspecified")
# identify sightings by day
rats_clean %>%
  count(sighting_weekday)
## # A tibble: 7 × 2
##   sighting_weekday     n
##   <ord>            <int>
## 1 Sunday            9084
## 2 Monday           17636
## 3 Tuesday          17507
## 4 Wednesday        17276
## 5 Thursday         16491
## 6 Friday           14581
## 7 Saturday          9338
# Assign a summarized data frame to an object to use it in a plot
rats_by_weekday <- rats_clean %>%
  count(sighting_weekday, sighting_year)

ggplot(rats_by_weekday, aes(y = fct_rev(sighting_weekday), x = n)) + 
  geom_col() +
  facet_wrap(vars(sighting_year))

# See the count of rat sightings by weekday and borough
rats_clean %>%
  count(sighting_weekday, borough, sighting_year)
## # A tibble: 280 × 4
##    sighting_weekday borough  sighting_year     n
##    <ord>            <chr>            <dbl> <int>
##  1 Sunday           BRONX             2010   159
##  2 Sunday           BRONX             2011   145
##  3 Sunday           BRONX             2012   181
##  4 Sunday           BRONX             2013   166
##  5 Sunday           BRONX             2014   224
##  6 Sunday           BRONX             2015   270
##  7 Sunday           BRONX             2016   314
##  8 Sunday           BRONX             2017   262
##  9 Sunday           BROOKLYN          2010   267
## 10 Sunday           BROOKLYN          2011   261
## # ℹ 270 more rows
# An alternative to count() is to specify the groups with group_by() and then
# be explicit about how you're summarizing the groups, such as calculating the
# mean, standard deviation, or number of observations (we do that here with `n()`).
rats_clean %>%
  group_by(sighting_weekday, borough) %>%
  summarize(n = n())
## `summarise()` has grouped output by 'sighting_weekday'. You can override using
## the `.groups` argument.
## # A tibble: 35 × 3
## # Groups:   sighting_weekday [7]
##    sighting_weekday borough           n
##    <ord>            <chr>         <int>
##  1 Sunday           BRONX          1721
##  2 Sunday           BROOKLYN       3109
##  3 Sunday           MANHATTAN      2626
##  4 Sunday           QUEENS         1216
##  5 Sunday           STATEN ISLAND   412
##  6 Monday           BRONX          3601
##  7 Monday           BROOKLYN       6066
##  8 Monday           MANHATTAN      4630
##  9 Monday           QUEENS         2488
## 10 Monday           STATEN ISLAND   851
## # ℹ 25 more rows
  # Filter data for the year 2010 to 2016
rats_filtered <- rats_clean %>%
  filter(sighting_year >= 2010 & sighting_year <= 2016)

# Summarize the data by borough and sighting year
summary_by_borough_year <- rats_filtered %>%
  group_by(borough, sighting_year) %>%
  summarize(total_sightings = n())
## `summarise()` has grouped output by 'borough'. You can override using the
## `.groups` argument.
# Print the summary
print("Summary of Rat Sightings by Borough and Year:")
## [1] "Summary of Rat Sightings by Borough and Year:"
print(summary_by_borough_year)
## # A tibble: 35 × 3
## # Groups:   borough [5]
##    borough  sighting_year total_sightings
##    <chr>            <dbl>           <int>
##  1 BRONX             2010            2067
##  2 BRONX             2011            2174
##  3 BRONX             2012            2125
##  4 BRONX             2013            2120
##  5 BRONX             2014            2743
##  6 BRONX             2015            3189
##  7 BRONX             2016            3497
##  8 BROOKLYN          2010            3399
##  9 BROOKLYN          2011            3671
## 10 BROOKLYN          2012            3600
## # ℹ 25 more rows