library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── 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(sf)
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(ggplot2)
library(dplyr)

census <- sf::st_read('/Users/areeba/Desktop/tl_2024_us_zcta520/tl_2024_us_zcta520.shp')
## Reading layer `tl_2024_us_zcta520' from data source 
##   `/Users/areeba/Desktop/tl_2024_us_zcta520/tl_2024_us_zcta520.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 33791 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -176.6967 ymin: -14.37378 xmax: 145.8305 ymax: 71.34132
## Geodetic CRS:  NAD83
nyc_zip_codes <- c(10001:10282,   10451:10475,   11201:11256, 11004:11005, 11101:11106, 11354:11375, 11377:11386, 11411:11436, 11691:11697,   10301:10314)

nyc_zip <- census %>% filter(as.numeric(ZCTA5CE20) %in% nyc_zip_codes)
nrow(nyc_zip)
## [1] 208
acs_poverty_rate <- sf::st_read('/Users/areeba/Desktop/ACSST5Y2024.S1701_2026-05-01T215153/ACSST5Y2024.S1701-Data.csv')
## Reading layer `ACSST5Y2024.S1701-Data' from data source 
##   `/Users/areeba/Desktop/ACSST5Y2024.S1701_2026-05-01T215153/ACSST5Y2024.S1701-Data.csv' 
##   using driver `CSV'
## Warning: no simple feature geometries present: returning a data.frame or tbl_df
colnames(acs_poverty_rate)
##   [1] "GEO_ID"         "NAME"           "S1701_C01_001E" "S1701_C01_001M"
##   [5] "S1701_C01_002E" "S1701_C01_002M" "S1701_C01_003E" "S1701_C01_003M"
##   [9] "S1701_C01_004E" "S1701_C01_004M" "S1701_C01_005E" "S1701_C01_005M"
##  [13] "S1701_C01_006E" "S1701_C01_006M" "S1701_C01_007E" "S1701_C01_007M"
##  [17] "S1701_C01_008E" "S1701_C01_008M" "S1701_C01_009E" "S1701_C01_009M"
##  [21] "S1701_C01_010E" "S1701_C01_010M" "S1701_C01_011E" "S1701_C01_011M"
##  [25] "S1701_C01_012E" "S1701_C01_012M" "S1701_C01_013E" "S1701_C01_013M"
##  [29] "S1701_C01_014E" "S1701_C01_014M" "S1701_C01_015E" "S1701_C01_015M"
##  [33] "S1701_C01_016E" "S1701_C01_016M" "S1701_C01_017E" "S1701_C01_017M"
##  [37] "S1701_C01_018E" "S1701_C01_018M" "S1701_C01_019E" "S1701_C01_019M"
##  [41] "S1701_C01_020E" "S1701_C01_020M" "S1701_C01_021E" "S1701_C01_021M"
##  [45] "S1701_C01_022E" "S1701_C01_022M" "S1701_C01_023E" "S1701_C01_023M"
##  [49] "S1701_C01_024E" "S1701_C01_024M" "S1701_C01_025E" "S1701_C01_025M"
##  [53] "S1701_C01_026E" "S1701_C01_026M" "S1701_C01_027E" "S1701_C01_027M"
##  [57] "S1701_C01_028E" "S1701_C01_028M" "S1701_C01_029E" "S1701_C01_029M"
##  [61] "S1701_C01_030E" "S1701_C01_030M" "S1701_C01_031E" "S1701_C01_031M"
##  [65] "S1701_C01_032E" "S1701_C01_032M" "S1701_C01_033E" "S1701_C01_033M"
##  [69] "S1701_C01_034E" "S1701_C01_034M" "S1701_C01_035E" "S1701_C01_035M"
##  [73] "S1701_C01_036E" "S1701_C01_036M" "S1701_C01_037E" "S1701_C01_037M"
##  [77] "S1701_C01_038E" "S1701_C01_038M" "S1701_C01_039E" "S1701_C01_039M"
##  [81] "S1701_C01_040E" "S1701_C01_040M" "S1701_C01_041E" "S1701_C01_041M"
##  [85] "S1701_C01_042E" "S1701_C01_042M" "S1701_C01_043E" "S1701_C01_043M"
##  [89] "S1701_C01_044E" "S1701_C01_044M" "S1701_C01_045E" "S1701_C01_045M"
##  [93] "S1701_C01_046E" "S1701_C01_046M" "S1701_C01_047E" "S1701_C01_047M"
##  [97] "S1701_C01_048E" "S1701_C01_048M" "S1701_C01_049E" "S1701_C01_049M"
## [101] "S1701_C01_050E" "S1701_C01_050M" "S1701_C01_051E" "S1701_C01_051M"
## [105] "S1701_C01_052E" "S1701_C01_052M" "S1701_C01_053E" "S1701_C01_053M"
## [109] "S1701_C01_054E" "S1701_C01_054M" "S1701_C01_055E" "S1701_C01_055M"
## [113] "S1701_C01_056E" "S1701_C01_056M" "S1701_C01_057E" "S1701_C01_057M"
## [117] "S1701_C01_058E" "S1701_C01_058M" "S1701_C01_059E" "S1701_C01_059M"
## [121] "S1701_C01_060E" "S1701_C01_060M" "S1701_C01_061E" "S1701_C01_061M"
## [125] "S1701_C01_062E" "S1701_C01_062M" "S1701_C02_001E" "S1701_C02_001M"
## [129] "S1701_C02_002E" "S1701_C02_002M" "S1701_C02_003E" "S1701_C02_003M"
## [133] "S1701_C02_004E" "S1701_C02_004M" "S1701_C02_005E" "S1701_C02_005M"
## [137] "S1701_C02_006E" "S1701_C02_006M" "S1701_C02_007E" "S1701_C02_007M"
## [141] "S1701_C02_008E" "S1701_C02_008M" "S1701_C02_009E" "S1701_C02_009M"
## [145] "S1701_C02_010E" "S1701_C02_010M" "S1701_C02_011E" "S1701_C02_011M"
## [149] "S1701_C02_012E" "S1701_C02_012M" "S1701_C02_013E" "S1701_C02_013M"
## [153] "S1701_C02_014E" "S1701_C02_014M" "S1701_C02_015E" "S1701_C02_015M"
## [157] "S1701_C02_016E" "S1701_C02_016M" "S1701_C02_017E" "S1701_C02_017M"
## [161] "S1701_C02_018E" "S1701_C02_018M" "S1701_C02_019E" "S1701_C02_019M"
## [165] "S1701_C02_020E" "S1701_C02_020M" "S1701_C02_021E" "S1701_C02_021M"
## [169] "S1701_C02_022E" "S1701_C02_022M" "S1701_C02_023E" "S1701_C02_023M"
## [173] "S1701_C02_024E" "S1701_C02_024M" "S1701_C02_025E" "S1701_C02_025M"
## [177] "S1701_C02_026E" "S1701_C02_026M" "S1701_C02_027E" "S1701_C02_027M"
## [181] "S1701_C02_028E" "S1701_C02_028M" "S1701_C02_029E" "S1701_C02_029M"
## [185] "S1701_C02_030E" "S1701_C02_030M" "S1701_C02_031E" "S1701_C02_031M"
## [189] "S1701_C02_032E" "S1701_C02_032M" "S1701_C02_033E" "S1701_C02_033M"
## [193] "S1701_C02_034E" "S1701_C02_034M" "S1701_C02_035E" "S1701_C02_035M"
## [197] "S1701_C02_036E" "S1701_C02_036M" "S1701_C02_037E" "S1701_C02_037M"
## [201] "S1701_C02_038E" "S1701_C02_038M" "S1701_C02_039E" "S1701_C02_039M"
## [205] "S1701_C02_040E" "S1701_C02_040M" "S1701_C02_041E" "S1701_C02_041M"
## [209] "S1701_C02_042E" "S1701_C02_042M" "S1701_C02_043E" "S1701_C02_043M"
## [213] "S1701_C02_044E" "S1701_C02_044M" "S1701_C02_045E" "S1701_C02_045M"
## [217] "S1701_C02_046E" "S1701_C02_046M" "S1701_C02_047E" "S1701_C02_047M"
## [221] "S1701_C02_048E" "S1701_C02_048M" "S1701_C02_049E" "S1701_C02_049M"
## [225] "S1701_C02_050E" "S1701_C02_050M" "S1701_C02_051E" "S1701_C02_051M"
## [229] "S1701_C02_052E" "S1701_C02_052M" "S1701_C02_053E" "S1701_C02_053M"
## [233] "S1701_C02_054E" "S1701_C02_054M" "S1701_C02_055E" "S1701_C02_055M"
## [237] "S1701_C02_056E" "S1701_C02_056M" "S1701_C02_057E" "S1701_C02_057M"
## [241] "S1701_C02_058E" "S1701_C02_058M" "S1701_C02_059E" "S1701_C02_059M"
## [245] "S1701_C02_060E" "S1701_C02_060M" "S1701_C02_061E" "S1701_C02_061M"
## [249] "S1701_C02_062E" "S1701_C02_062M" "S1701_C03_001E" "S1701_C03_001M"
## [253] "S1701_C03_002E" "S1701_C03_002M" "S1701_C03_003E" "S1701_C03_003M"
## [257] "S1701_C03_004E" "S1701_C03_004M" "S1701_C03_005E" "S1701_C03_005M"
## [261] "S1701_C03_006E" "S1701_C03_006M" "S1701_C03_007E" "S1701_C03_007M"
## [265] "S1701_C03_008E" "S1701_C03_008M" "S1701_C03_009E" "S1701_C03_009M"
## [269] "S1701_C03_010E" "S1701_C03_010M" "S1701_C03_011E" "S1701_C03_011M"
## [273] "S1701_C03_012E" "S1701_C03_012M" "S1701_C03_013E" "S1701_C03_013M"
## [277] "S1701_C03_014E" "S1701_C03_014M" "S1701_C03_015E" "S1701_C03_015M"
## [281] "S1701_C03_016E" "S1701_C03_016M" "S1701_C03_017E" "S1701_C03_017M"
## [285] "S1701_C03_018E" "S1701_C03_018M" "S1701_C03_019E" "S1701_C03_019M"
## [289] "S1701_C03_020E" "S1701_C03_020M" "S1701_C03_021E" "S1701_C03_021M"
## [293] "S1701_C03_022E" "S1701_C03_022M" "S1701_C03_023E" "S1701_C03_023M"
## [297] "S1701_C03_024E" "S1701_C03_024M" "S1701_C03_025E" "S1701_C03_025M"
## [301] "S1701_C03_026E" "S1701_C03_026M" "S1701_C03_027E" "S1701_C03_027M"
## [305] "S1701_C03_028E" "S1701_C03_028M" "S1701_C03_029E" "S1701_C03_029M"
## [309] "S1701_C03_030E" "S1701_C03_030M" "S1701_C03_031E" "S1701_C03_031M"
## [313] "S1701_C03_032E" "S1701_C03_032M" "S1701_C03_033E" "S1701_C03_033M"
## [317] "S1701_C03_034E" "S1701_C03_034M" "S1701_C03_035E" "S1701_C03_035M"
## [321] "S1701_C03_036E" "S1701_C03_036M" "S1701_C03_037E" "S1701_C03_037M"
## [325] "S1701_C03_038E" "S1701_C03_038M" "S1701_C03_039E" "S1701_C03_039M"
## [329] "S1701_C03_040E" "S1701_C03_040M" "S1701_C03_041E" "S1701_C03_041M"
## [333] "S1701_C03_042E" "S1701_C03_042M" "S1701_C03_043E" "S1701_C03_043M"
## [337] "S1701_C03_044E" "S1701_C03_044M" "S1701_C03_045E" "S1701_C03_045M"
## [341] "S1701_C03_046E" "S1701_C03_046M" "S1701_C03_047E" "S1701_C03_047M"
## [345] "S1701_C03_048E" "S1701_C03_048M" "S1701_C03_049E" "S1701_C03_049M"
## [349] "S1701_C03_050E" "S1701_C03_050M" "S1701_C03_051E" "S1701_C03_051M"
## [353] "S1701_C03_052E" "S1701_C03_052M" "S1701_C03_053E" "S1701_C03_053M"
## [357] "S1701_C03_054E" "S1701_C03_054M" "S1701_C03_055E" "S1701_C03_055M"
## [361] "S1701_C03_056E" "S1701_C03_056M" "S1701_C03_057E" "S1701_C03_057M"
## [365] "S1701_C03_058E" "S1701_C03_058M" "S1701_C03_059E" "S1701_C03_059M"
## [369] "S1701_C03_060E" "S1701_C03_060M" "S1701_C03_061E" "S1701_C03_061M"
## [373] "S1701_C03_062E" "S1701_C03_062M" "field_375"
acs_median_household_income <- sf::st_read('/Users/areeba/Desktop/ACSDT5Y2024.B19013_2026-05-01T215438/ACSDT5Y2024.B19013-Data.csv')
## Reading layer `ACSDT5Y2024.B19013-Data' from data source 
##   `/Users/areeba/Desktop/ACSDT5Y2024.B19013_2026-05-01T215438/ACSDT5Y2024.B19013-Data.csv' 
##   using driver `CSV'
## Warning: no simple feature geometries present: returning a data.frame or tbl_df
colnames(acs_median_household_income)
## [1] "GEO_ID"      "NAME"        "B19013_001E" "B19013_001M" "field_5"
acs_race <- sf::st_read('/Users/areeba/Desktop/ACSDT5Y2024.B02001_2026-05-01T215624/ACSDT5Y2024.B02001-Data.csv')
## Reading layer `ACSDT5Y2024.B02001-Data' from data source 
##   `/Users/areeba/Desktop/ACSDT5Y2024.B02001_2026-05-01T215624/ACSDT5Y2024.B02001-Data.csv' 
##   using driver `CSV'
## Warning: no simple feature geometries present: returning a data.frame or tbl_df
colnames(acs_race)
##  [1] "GEO_ID"      "NAME"        "B02001_001E" "B02001_001M" "B02001_002E"
##  [6] "B02001_002M" "B02001_003E" "B02001_003M" "B02001_004E" "B02001_004M"
## [11] "B02001_005E" "B02001_005M" "B02001_006E" "B02001_006M" "B02001_007E"
## [16] "B02001_007M" "B02001_008E" "B02001_008M" "B02001_009E" "B02001_009M"
## [21] "B02001_010E" "B02001_010M" "field_23"
acs_poverty_rate <- acs_poverty_rate %>% select(GEO_ID, NAME, S1701_C01_001E, S1701_C02_001E, S1701_C03_001E)

acs_median_household_income <- acs_median_household_income %>% select(GEO_ID, NAME, B19013_001E)

acs_race <- acs_race %>% select(GEO_ID, NAME, B02001_001E, B02001_002E, B02001_003E, B02001_005E, B02001_007E, B02001_008E)

acs_combined <- acs_race %>% left_join(acs_median_household_income, by = "NAME") %>% left_join(acs_poverty_rate, by="NAME")

glimpse(acs_combined)
## Rows: 1,825
## Columns: 14
## $ GEO_ID.x       <chr> "Geography", "860Z200US06390", "860Z200US10001", "860Z2…
## $ NAME           <chr> "Geographic Area Name", "ZCTA5 06390", "ZCTA5 10001", "…
## $ B02001_001E    <chr> "Estimate!!Total:", "72", "30511", "76873", "53560", "3…
## $ B02001_002E    <chr> "Estimate!!Total:!!White alone", "48", "15452", "22760"…
## $ B02001_003E    <chr> "Estimate!!Total:!!Black or African American alone", "0…
## $ B02001_005E    <chr> "Estimate!!Total:!!Asian alone", "0", "6010", "27854", …
## $ B02001_007E    <chr> "Estimate!!Total:!!Some Other Race alone", "6", "1971",…
## $ B02001_008E    <chr> "Estimate!!Total:!!Two or More Races:", "7", "3607", "9…
## $ GEO_ID.y       <chr> "Geography", "860Z200US06390", "860Z200US10001", "860Z2…
## $ B19013_001E    <chr> "Estimate!!Median household income in the past 12 month…
## $ GEO_ID         <chr> "Geography", "860Z200US06390", "860Z200US10001", "860Z2…
## $ S1701_C01_001E <chr> "Estimate!!Total!!Population for whom poverty status is…
## $ S1701_C02_001E <chr> "Estimate!!Below poverty level!!Population for whom pov…
## $ S1701_C03_001E <chr> "Estimate!!Percent below poverty level!!Population for …
acs_combined <- acs_combined %>% mutate(NAME = str_remove(NAME, "ZCTA5 "))
head(acs_combined$NAME)
## [1] "Geographic Area Name" "06390"                "10001"               
## [4] "10002"                "10003"                "10004"
ems_dispatch <- sf::st_read('/Users/areeba/Desktop/EMS_Incident_Dispatch_Data_20260501.csv')
## Reading layer `EMS_Incident_Dispatch_Data_20260501' from data source 
##   `/Users/areeba/Desktop/EMS_Incident_Dispatch_Data_20260501.csv' 
##   using driver `CSV'
## Warning: no simple feature geometries present: returning a data.frame or tbl_df
ems_dispatch <- ems_dispatch %>% mutate(ZIPCODE = as.character(ZIPCODE))
acs_combined <- acs_combined %>% mutate(NAME = as.character(NAME))

ems_census <- ems_dispatch %>% left_join(acs_combined, by = c("ZIPCODE" = "NAME"))

nrow(ems_dispatch)
## [1] 289900
nrow(ems_census)
## [1] 289900
colnames(ems_census)
##  [1] "CAD_INCIDENT_ID"                "INCIDENT_DATETIME"             
##  [3] "INITIAL_CALL_TYPE"              "INITIAL_SEVERITY_LEVEL_CODE"   
##  [5] "FINAL_CALL_TYPE"                "FINAL_SEVERITY_LEVEL_CODE"     
##  [7] "FIRST_ASSIGNMENT_DATETIME"      "VALID_DISPATCH_RSPNS_TIME_INDC"
##  [9] "DISPATCH_RESPONSE_SECONDS_QY"   "FIRST_ACTIVATION_DATETIME"     
## [11] "FIRST_ON_SCENE_DATETIME"        "VALID_INCIDENT_RSPNS_TIME_INDC"
## [13] "INCIDENT_RESPONSE_SECONDS_QY"   "INCIDENT_TRAVEL_TM_SECONDS_QY" 
## [15] "FIRST_TO_HOSP_DATETIME"         "FIRST_HOSP_ARRIVAL_DATETIME"   
## [17] "INCIDENT_CLOSE_DATETIME"        "HELD_INDICATOR"                
## [19] "INCIDENT_DISPOSITION_CODE"      "BOROUGH"                       
## [21] "INCIDENT_DISPATCH_AREA"         "ZIPCODE"                       
## [23] "POLICEPRECINCT"                 "CITYCOUNCILDISTRICT"           
## [25] "COMMUNITYDISTRICT"              "COMMUNITYSCHOOLDISTRICT"       
## [27] "CONGRESSIONALDISTRICT"          "REOPEN_INDICATOR"              
## [29] "SPECIAL_EVENT_INDICATOR"        "STANDBY_INDICATOR"             
## [31] "TRANSFER_INDICATOR"             "GEO_ID.x"                      
## [33] "B02001_001E"                    "B02001_002E"                   
## [35] "B02001_003E"                    "B02001_005E"                   
## [37] "B02001_007E"                    "B02001_008E"                   
## [39] "GEO_ID.y"                       "B19013_001E"                   
## [41] "GEO_ID"                         "S1701_C01_001E"                
## [43] "S1701_C02_001E"                 "S1701_C03_001E"
ems_census %>%
  group_by(BOROUGH) %>%
  summarise(call_volume = n()) %>%
  ggplot(aes(x = reorder(BOROUGH, -call_volume), y = call_volume, fill = BOROUGH)) +
  geom_bar(stat = "identity", fill = "navy") +
  labs(title = "EMS Asthma related calls",
       x = "Borough",
       y = "Number of Calls") +
  theme_minimal()