Author

Naomi Thornhill

Published

March 29, 2025

read zipcode and covid data

Show the code
zipcode_sf<-st_read("Section_08/R-Spatial_II_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
Reading layer `ZIP_CODE_040114' from data source 
  `/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 263 features and 12 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
Projected CRS: NAD83 / New York Long Island (ftUS)
Show the code
covid_data<-st_read("Section_08/R-Spatial_II_Lab/tests-by-zcta_2020_04_19.csv")
Reading layer `tests-by-zcta_2020_04_19' from data source 
  `/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/tests-by-zcta_2020_04_19.csv' 
  using driver `CSV'
Warning: no simple feature geometries present: returning a data.frame or tbl_df

merge COVID data with zipcode data

Show the code
covid_zipcode<-merge(zipcode_sf,covid_data,by.x ='ZIPCODE', by.y = 'MODZCTA')

read food store data

Show the code
store_sf<-st_read("/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/nycFoodStore.shp")
Reading layer `nycFoodStore' from data source 
  `/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/nycFoodStore.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 11300 features and 16 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -74.2484 ymin: 40.50782 xmax: -73.67061 ymax: 40.91008
Geodetic CRS:  WGS 84

aggregate zip code with food store

Show the code
store_sf<-st_read("/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/nycFoodStore.shp")
Reading layer `nycFoodStore' from data source 
  `/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/nycFoodStore.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 11300 features and 16 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -74.2484 ymin: 40.50782 xmax: -73.67061 ymax: 40.91008
Geodetic CRS:  WGS 84
Show the code
food_retail_sf<-st_transform(store_sf,st_crs(zipcode_sf))
store_zip_intersect <- st_join(food_retail_sf, zipcode_sf)
store_zip_count<-aggregate(store_zip_intersect,by = list(store_zip_intersect$ZIPCODE),FUN = length)

read health facility

Show the code
health_sf<-st_read("/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/NYS_Health_Facility.csv")
Reading layer `NYS_Health_Facility' from data source 
  `/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/NYS_Health_Facility.csv' 
  using driver `CSV'
Warning: no simple feature geometries present: returning a data.frame or tbl_df
Show the code
str(health_sf)
'data.frame':   3990 obs. of  36 variables:
 $ Facility.ID                 : chr  "204" "620" "654" "1156" ...
 $ Facility.Name               : chr  "Hospice at Lourdes" "Charles T Sitrin Health Care Center Inc" "Central Park Rehabilitation and Nursing Center" "East Side Nursing Home" ...
 $ Short.Description           : chr  "HSPC" "NH" "NH" "NH" ...
 $ Description                 : chr  "Hospice" "Residential Health Care Facility - SNF" "Residential Health Care Facility - SNF" "Residential Health Care Facility - SNF" ...
 $ Facility.Open.Date          : chr  "06/01/1985" "02/01/1989" "02/01/1989" "08/01/1979" ...
 $ Facility.Address.1          : chr  "4102 Old Vestal Road" "2050 Tilden Avenue" "116 Martin Luther King East" "62 Prospect St" ...
 $ Facility.Address.2          : chr  "" "" "" "" ...
 $ Facility.City               : chr  "Vestal" "New Hartford" "Syracuse" "Warsaw" ...
 $ Facility.State              : chr  "New York" "New York" "New York" "New York" ...
 $ Facility.Zip.Code           : chr  "13850" "13413" "13205" "14569" ...
 $ Facility.Phone.Number       : chr  "6077985692" "3157973114" "3154751641" "5857868151" ...
 $ Facility.Fax.Number         : chr  "" "" "" "" ...
 $ Facility.Website            : chr  "" "" "" "" ...
 $ Facility.County.Code        : chr  "3" "32" "33" "60" ...
 $ Facility.County             : chr  "Broome" "Oneida" "Onondaga" "Wyoming" ...
 $ Regional.Office.ID          : chr  "3" "3" "3" "1" ...
 $ Regional.Office             : chr  "Central New York Regional Office" "Central New York Regional Office" "Central New York Regional Office" "Western Regional Office - Buffalo" ...
 $ Main.Site.Name              : chr  "" "" "" "" ...
 $ Main.Site.Facility.ID       : chr  "" "" "" "" ...
 $ Operating.Certificate.Number: chr  "0301501F" "3227304N" "3301326N" "6027303N" ...
 $ Operator.Name               : chr  "Our Lady of Lourdes Memorial Hospital Inc" "Charles T Sitrin Health Care Center, Inc" "CPRNC, LLC" "East Side Nursing Home Inc" ...
 $ Operator.Address.1          : chr  "169 Riverside Drive" "Box 1000 Tilden Avenue" "116 Martin Luther King East" "62 Prospect Street" ...
 $ Operator.Address.2          : chr  "" "" "" "" ...
 $ Operator.City               : chr  "Binghamton" "New Hartford" "Syracuse" "Warsaw" ...
 $ Operator.State              : chr  "New York" "New York" "New York" "New York" ...
 $ Operator.Zip.Code           : chr  "13905" "13413" "13205" "14569" ...
 $ Cooperator.Name             : chr  "" "" "" "" ...
 $ Cooperator.Address          : chr  "" "" "" "" ...
 $ Cooperator.Address.2        : chr  "" "" "" "" ...
 $ Cooperator.City             : chr  "" "" "" "" ...
 $ Cooperator.State            : chr  "New York" "New York" "New York" "New York" ...
 $ Cooperator.Zip.Code         : chr  "" "" "" "" ...
 $ Ownership.Type              : chr  "Not for Profit Corporation" "Not for Profit Corporation" "LLC" "Business Corporation" ...
 $ Facility.Latitude           : chr  "42.097095" "43.05497" "" "42.738979" ...
 $ Facility.Longitude          : chr  "-75.975243" "-75.228828" "" "-78.12867" ...
 $ Facility.Location           : chr  "(42.097095, -75.975243)" "(43.05497, -75.228828)" "" "(42.738979, -78.12867)" ...

remove any blank lines

Show the code
health_sf$Facility.Latitude[health_sf$Facility.Latitude == ""] <- NA
health_sf$Facility.Longitude[health_sf$Facility.Longitude == ""] <- NA
health_sf<-health_sf[!is.na(health_sf$Facility.Latitude) & !is.na(health_sf$Facility.Longitude),]

aggregate health facilities with zipcode

Show the code
health_sf<-st_as_sf(health_sf,coords = c("Facility.Latitude","Facility.Longitude"),crs=4326)
health_sf <- dplyr::filter(health_sf, Short.Description == "HOSP")
health_sf<- sf::st_transform(health_sf, st_crs(zipcode_sf))
health_sf<-sf::st_join(zipcode_sf,health_sf)

read census and acs file

Show the code
nycCensus<-sf::st_read('Section_08/R-Spatial_II_Lab/2010 Census Tracts/geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp', stringsAsFactors = FALSE)
Reading layer `geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a' from data source `/Users/nat/Downloads/Rstudio HW/R-Spatial Labs/Lab 8/Section_08/R-Spatial_II_Lab/2010 Census Tracts/geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 2165 features and 11 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
Geodetic CRS:  WGS84(DD)
Show the code
nycCensus%<>%dplyr::mutate(cntyFIPS = case_when(
  boro_name == 'Bronx' ~ '005',
  boro_name == 'Brooklyn' ~ '047',
  boro_name == 'Manhattan' ~ '061',
  boro_name == 'Queens' ~ '081',
  boro_name == 'Staten Island' ~ '085'),
  tractFIPS = paste(cntyFIPS, ct2010, sep=''))
acsData <- readLines("Section_08/R-Spatial_II_Lab/ACSDP5Y2018.DP05_data_with_overlays_2020-04-22T132935.csv") %>%
  magrittr::extract(-2) %>% 
  textConnection() %>%
  read.csv(header=TRUE, quote= "\"") %>%
  dplyr::select(GEO_ID, 
                totPop = DP05_0001E, elderlyPop = DP05_0024E, # >= 65
                malePop = DP05_0002E, femalePop = DP05_0003E,  
                whitePop = DP05_0037E, blackPop = DP05_0038E,
                asianPop = DP05_0067E, hispanicPop = DP05_0071E,
                adultPop = DP05_0021E, citizenAdult = DP05_0087E) %>%
  dplyr::mutate(censusCode = stringr::str_sub(GEO_ID, -9,-1));

acsData %>%
  magrittr::extract(1:10,)
                 GEO_ID totPop elderlyPop malePop femalePop whitePop blackPop
1  1400000US36005000100   7080         51    6503       577     1773     4239
2  1400000US36005000200   4542        950    2264      2278     2165     1279
3  1400000US36005000400   5634        710    2807      2827     2623     1699
4  1400000US36005001600   5917        989    2365      3552     2406     2434
5  1400000US36005001900   2765         76    1363      1402      585     1041
6  1400000US36005002000   9409        977    4119      5290     3185     4487
7  1400000US36005002300   4600        648    2175      2425      479     2122
8  1400000US36005002400    172          0     121        51       69       89
9  1400000US36005002500   5887        548    2958      2929      903     1344
10 1400000US36005002701   2868        243    1259      1609      243      987
   asianPop hispanicPop adultPop citizenAdult censusCode
1       130        2329     6909         6100  005000100
2       119        3367     3582         2952  005000200
3       226        3873     4507         4214  005000400
4        68        3603     4416         3851  005001600
5       130        1413     2008         1787  005001900
6        29        5905     6851         6170  005002000
7        27        2674     3498         3056  005002300
8        14           0      131           42  005002400
9        68        4562     4237         2722  005002500
10        0        1985     1848         1412  005002701
Show the code
popData <- merge(nycCensus, acsData, by.x ='tractFIPS', by.y = 'censusCode')

aggregate acs data with zip code

Show the code
popNYC <- sf::st_transform(popData, st_crs(covid_zipcode))
covidPopZipNYC <- sf::st_join(covid_zipcode,popNYC)
sum(covidPopZipNYC$totPop)
[1] 15131579