R Spatial Lab Assignment #2

Task 1: Load Week 7 sf Objects

nyc_zip_sf <- st_read("lab_07_data.gpkg", layer = "nyc_zip_codes")
## Reading layer `nyc_zip_codes' from data source 
##   `/Users/elinorgoldsmith-greenberg/Documents/GTECH 385 Assignments/R-Spatial/data/Section_08/R-Spatial_II_Lab/lab_07_data.gpkg' 
##   using driver `GPKG'
## 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)
health_sf  <- st_read("lab_07_data.gpkg", layer = "health_facilities")
## Reading layer `health_facilities' from data source 
##   `/Users/elinorgoldsmith-greenberg/Documents/GTECH 385 Assignments/R-Spatial/data/Section_08/R-Spatial_II_Lab/lab_07_data.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 3843 features and 34 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -79.6299 ymin: 40.51677 xmax: -72.17 ymax: 44.97849
## Geodetic CRS:  WGS 84
retail_sf  <- st_read("lab_07_data.gpkg", layer = "retail_food_stores")
## Reading layer `retail_food_stores' from data source 
##   `/Users/elinorgoldsmith-greenberg/Documents/GTECH 385 Assignments/R-Spatial/data/Section_08/R-Spatial_II_Lab/lab_07_data.gpkg' 
##   using driver `GPKG'
## 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
print(nyc_zip_sf)
## 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)
## First 10 features:
##    ZIPCODE BLDGZIP  PO_NAME POPULATION     AREA STATE COUNTY ST_FIPS CTY_FIPS
## 1    11436       0  Jamaica      18681 22699295    NY Queens      36      081
## 2    11213       0 Brooklyn      62426 29631004    NY  Kings      36      047
## 3    11212       0 Brooklyn      83866 41972104    NY  Kings      36      047
## 4    11225       0 Brooklyn      56527 23698630    NY  Kings      36      047
## 5    11218       0 Brooklyn      72280 36868799    NY  Kings      36      047
## 6    11226       0 Brooklyn     106132 39408598    NY  Kings      36      047
## 7    11219       0 Brooklyn      92561 42002738    NY  Kings      36      047
## 8    11210       0 Brooklyn      67067 47887023    NY  Kings      36      047
## 9    11230       0 Brooklyn      80857 49926703    NY  Kings      36      047
## 10   11204       0 Brooklyn      77354 43555185    NY  Kings      36      047
##                     URL SHAPE_AREA SHAPE_LEN                           geom
## 1  http://www.usps.com/          0         0 POLYGON ((1038098 188138.4,...
## 2  http://www.usps.com/          0         0 POLYGON ((1001614 186926.4,...
## 3  http://www.usps.com/          0         0 POLYGON ((1011174 183696.3,...
## 4  http://www.usps.com/          0         0 POLYGON ((995908.4 183617.6...
## 5  http://www.usps.com/          0         0 POLYGON ((991997.1 176307.5...
## 6  http://www.usps.com/          0         0 POLYGON ((994821.5 177865.7...
## 7  http://www.usps.com/          0         0 POLYGON ((987286.4 173946.5...
## 8  http://www.usps.com/          0         0 POLYGON ((995796 171110.1, ...
## 9  http://www.usps.com/          0         0 POLYGON ((994099.3 171240.7...
## 10 http://www.usps.com/          0         0 POLYGON ((989500.2 170730.2...
print(health_sf)
## Simple feature collection with 3843 features and 34 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -79.6299 ymin: 40.51677 xmax: -72.17 ymax: 44.97849
## Geodetic CRS:  WGS 84
## First 10 features:
##    Facility.ID                           Facility.Name Short.Description
## 1          204                      Hospice at Lourdes              HSPC
## 2          620 Charles T Sitrin Health Care Center Inc                NH
## 3         1156                  East Side Nursing Home                NH
## 4         2589            Wellsville Manor Care Center                NH
## 5         3455       Harris Hill Nursing Facility, LLC                NH
## 6         3853                Garden City Surgi Center               DTC
## 7         4249                                Willcare              CHHA
## 8         4473                   Good Shepherd Hospice              HSPC
## 9         6230                  NYU Langone Rutherford           HOSP-EC
## 10        6482    Endoscopy Center of Long Island, LLC               DTC
##                               Description Facility.Open.Date
## 1                                 Hospice         06/01/1985
## 2  Residential Health Care Facility - SNF         02/01/1989
## 3  Residential Health Care Facility - SNF         08/01/1979
## 4  Residential Health Care Facility - SNF         02/01/1989
## 5  Residential Health Care Facility - SNF         04/08/1992
## 6         Diagnostic and Treatment Center         04/07/2008
## 7            Certified Home Health Agency         05/15/1990
## 8                                 Hospice         09/01/2002
## 9               Hospital Extension Clinic         01/01/2006
## 10        Diagnostic and Treatment Center         01/20/2003
##                    Facility.Address.1 Facility.Address.2 Facility.City
## 1                4102 Old Vestal Road               <NA>        Vestal
## 2                  2050 Tilden Avenue               <NA>  New Hartford
## 3                      62 Prospect St               <NA>        Warsaw
## 4                  4192A Bolivar Road               <NA>    Wellsville
## 5                   2699 Wehrle Drive               <NA> Williamsville
## 6                       400 Endo Blvd               <NA>   Garden City
## 7                 346 Delaware Avenue               <NA>       Buffalo
## 8  110 Bi-County Boulevard, Suite 114               <NA>   Farmingdale
## 9                      305 Second Ave               <NA>      New York
## 10                 711 Stewart Avenue               <NA>   Garden City
##    Facility.State Facility.Zip.Code Facility.Phone.Number Facility.Fax.Number
## 1        New York             13850            6077985692                  NA
## 2        New York             13413            3157973114                  NA
## 3        New York             14569            5857868151                  NA
## 4        New York             14895            5855934400                  NA
## 5        New York             14221            7166323700                  NA
## 6        New York             11530            5168328504                  NA
## 7        New York             14202            7168567500                  NA
## 8        New York             11735            6314656300          6314656533
## 9        New York             10003            2125986570                  NA
## 10       New York             11530            5162273254                  NA
##    Facility.Website Facility.County.Code Facility.County Regional.Office.ID
## 1              <NA>                    3          Broome                  3
## 2              <NA>                   32          Oneida                  3
## 3              <NA>                   60         Wyoming                  1
## 4              <NA>                    2        Allegany                  1
## 5              <NA>                   14            Erie                  1
## 6              <NA>                   29          Nassau                  7
## 7              <NA>                   14            Erie                  1
## 8              <NA>                   29          Nassau                  7
## 9              <NA>                 7093        New York                  5
## 10             <NA>                   29          Nassau                  7
##                                      Regional.Office        Main.Site.Name
## 1                   Central New York Regional Office                  <NA>
## 2                   Central New York Regional Office                  <NA>
## 3                  Western Regional Office - Buffalo                  <NA>
## 4                  Western Regional Office - Buffalo                  <NA>
## 5                  Western Regional Office - Buffalo                  <NA>
## 6    Metropolitan Area Regional Office - Long Island                  <NA>
## 7                  Western Regional Office - Buffalo                  <NA>
## 8    Metropolitan Area Regional Office - Long Island                  <NA>
## 9  Metropolitan Area Regional Office - New York City NYU Langone Hospitals
## 10   Metropolitan Area Regional Office - Long Island                  <NA>
##    Main.Site.Facility.ID Operating.Certificate.Number
## 1                     NA                     0301501F
## 2                     NA                     3227304N
## 3                     NA                     6027303N
## 4                     NA                     0228305N
## 5                     NA                     1406301N
## 6                     NA                     2905204R
## 7                     NA                      1401606
## 8                     NA                     5151501F
## 9                   1463                     7002053H
## 10                    NA                     2905202R
##                                Operator.Name             Operator.Address.1
## 1  Our Lady of Lourdes Memorial Hospital Inc            169 Riverside Drive
## 2   Charles T Sitrin Health Care Center, Inc         Box 1000 Tilden Avenue
## 3                 East Side Nursing Home Inc             62 Prospect Street
## 4                       Wellsville Manor LLC             4192a Bolivar Road
## 5          Harris Hill Nursing Facility, LLC 560 Delaware Avenue, Suite 400
## 6                            Endo Group, LLC             400 Endo Boulevard
## 7      Western Region Health Corporation Inc            346 Delaware Avenue
## 8                      Good Shepherd Hospice        110 Bi-County Boulevard
## 9                      NYU Langone Hospitals               550 First Avenue
## 10      Endoscopy Center of Long Island, LLC             711 Stewart Avenue
##    Operator.Address.2 Operator.City Operator.State Operator.Zip.Code
## 1                <NA>    Binghamton       New York             13905
## 2                <NA>  New Hartford       New York             13413
## 3                <NA>        Warsaw       New York             14569
## 4                <NA>    Wellsville       New York             14897
## 5                <NA>       Buffalo       New York             14202
## 6                <NA>   Garden City       New York             11530
## 7                <NA>       Buffalo       New York             14202
## 8           Suite 114   Farmingdale       New York             11735
## 9                <NA>      New York       New York             10016
## 10               <NA>   Garden City       New York             11530
##    Cooperator.Name Cooperator.Address Cooperator.Address.2 Cooperator.City
## 1             <NA>               <NA>                 <NA>            <NA>
## 2             <NA>               <NA>                 <NA>            <NA>
## 3             <NA>               <NA>                 <NA>            <NA>
## 4             <NA>               <NA>                 <NA>            <NA>
## 5             <NA>               <NA>                 <NA>            <NA>
## 6             <NA>               <NA>                 <NA>            <NA>
## 7             <NA>               <NA>                 <NA>            <NA>
## 8             <NA>               <NA>                 <NA>            <NA>
## 9             <NA>               <NA>                 <NA>            <NA>
## 10            <NA>               <NA>                 <NA>            <NA>
##    Cooperator.State Cooperator.Zip.Code             Ownership.Type
## 1          New York                <NA> Not for Profit Corporation
## 2          New York                <NA> Not for Profit Corporation
## 3          New York                <NA>       Business Corporation
## 4          New York                <NA>                        LLC
## 5          New York                <NA>                        LLC
## 6          New York                <NA>                        LLC
## 7          New York                <NA>       Business Corporation
## 8          New York                <NA> Not for Profit Corporation
## 9          New York                <NA> Not for Profit Corporation
## 10         New York                <NA>                        LLC
##          Facility.Location                       geom
## 1  (42.097095, -75.975243)  POINT (-75.97524 42.0971)
## 2   (43.05497, -75.228828) POINT (-75.22883 43.05497)
## 3   (42.738979, -78.12867) POINT (-78.12867 42.73898)
## 4  (42.126461, -77.967834) POINT (-77.96783 42.12646)
## 5   (42.956268, -78.68856) POINT (-78.68856 42.95627)
## 6  (40.733765, -73.591286) POINT (-73.59129 40.73376)
## 7  (42.893864, -78.875824) POINT (-78.87582 42.89386)
## 8  (40.725994, -73.428329) POINT (-73.42833 40.72599)
## 9  (40.734818, -73.983231) POINT (-73.98323 40.73482)
## 10 (40.732571, -73.607971) POINT (-73.60797 40.73257)
print(retail_sf)
## 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
## First 10 features:
##    ï..County License.Number Operation.Type Establishment.Type
## 1      Bronx         734149          Store                JAC
## 2      Bronx         606221          Store                JAC
## 3      Bronx         606228          Store                JAC
## 4      Bronx         723375          Store                JAC
## 5      Bronx         724807          Store                JAC
## 6      Bronx         712943          Store                JAC
## 7      Bronx         703060          Store                JAC
## 8      Bronx         609065          Store                JAC
## 9      Bronx         722972          Store                  A
## 10     Bronx         609621          Store                JAC
##                          Entity.Name                DBA.Name Street.Number
## 1        7 ELEVEN FOOD STORE #37933H                    <NA>           500
## 2    1001 SAN MIGUEL FOOD CENTER INC 1001 SAN MIGUEL FD CNTR          1001
## 3                1029 FOOD PLAZA INC         1029 FOOD PLAZA           122
## 4             1078 DELI GROCERY CORP       1078 DELI GROCERY          1078
## 5        1086 LUNA DELI GROCERY CORP  1086 LUNA DELI GROCERY          1086
## 6           109 AJ DELI GROCERY CORP     109 AJ DELI GROCERY           109
## 7  10 NEIGHBORHOOD CANDY GROCERY COR 10 NEIGHBORHOOD CANDY G            10
## 8      1105 TINTON DELI GROCERY CORP   1105 TINTON DELI GRCY          1105
## 9          1150 WEBSTER PHARMACY INC 1150 WEBSTER PHARMACY I          1150
## 10           1158 GROCERY & DELI INC     1158 GROCERY & DELI          1158
##          Street.Name Address.Line.2 Address.Line.3  City State Zip.Code
## 1     BAYCHESTER AVE             NA             NA BRONX    NY    10475
## 2       SHERIDAN AVE             NA             NA BRONX    NY    10456
## 3         E 181ST ST             NA             NA BRONX    NY    10453
## 4  EAST 165TH STREET             NA             NA BRONX    NY    10459
## 5        BOSTON ROAD             NA             NA BRONX    NY    10456
## 6      E TREMONT AVE             NA             NA BRONX    NY    10453
## 7    W. GUN HILL RD.             NA             NA BRONX    NY    10467
## 8         TINTON AVE             NA             NA BRONX    NY    10456
## 9     WEBSTER AVENUE             NA             NA BRONX    NY    10456
## 10   ST LAWRENCE AVE             NA             NA BRONX    NY    10472
##    Square.Footage
## 1               0
## 2            1100
## 3            2000
## 4            1200
## 5            1500
## 6            2400
## 7            1000
## 8            1200
## 9            3400
## 10            500
##                                                                Location
## 1      500 BAYCHESTER AVE\r\nBRONX, NY 10475\r\n(40.869156, -73.831875)
## 2       1001 SHERIDAN AVE\r\nBRONX, NY 10456\r\n(40.829061, -73.919613)
## 3          122 E 181ST ST\r\nBRONX, NY 10453\r\n(40.854755, -73.902853)
## 4  1078 EAST 165TH STREET\r\nBRONX, NY 10459\r\n(40.825105, -73.890589)
## 5        1086 BOSTON ROAD\r\nBRONX, NY 10456\r\n(40.827096, -73.905123)
## 6       109 E TREMONT AVE\r\nBRONX, NY 10453\r\n(40.850537, -73.907137)
## 7        10 W GUN HILL RD\r\nBRONX, NY 10467\r\n(40.882869, -73.881552)
## 8         1105 TINTON AVE\r\nBRONX, NY 10456\r\n(40.826607, -73.901498)
## 9      1150 WEBSTER AVENUE\r\nBRONX, NY 10456\r\n(40.830425, -73.91063)
## 10   1158 ST LAWRENCE AVE\r\nBRONX, NY 10472\r\n(40.829105, -73.866678)
##                   Coords                       geom
## 1  40.869156, -73.831875 POINT (-73.83187 40.86916)
## 2  40.829061, -73.919613 POINT (-73.91961 40.82906)
## 3  40.854755, -73.902853 POINT (-73.90285 40.85475)
## 4  40.825105, -73.890589  POINT (-73.89059 40.8251)
## 5  40.827096, -73.905123  POINT (-73.90512 40.8271)
## 6  40.850537, -73.907137 POINT (-73.90714 40.85054)
## 7  40.882869, -73.881552 POINT (-73.88155 40.88287)
## 8  40.826607, -73.901498  POINT (-73.9015 40.82661)
## 9   40.830425, -73.91063 POINT (-73.91063 40.83042)
## 10 40.829105, -73.866678  POINT (-73.86668 40.8291)

Task 2: Join COVID-19 Data to NYC Zip Code Areas

covid_df <- read_csv("tests-by-zcta_2020_04_19.csv", show_col_types = FALSE)

print(covid_df)
## # A tibble: 178 × 4
##    MODZCTA Positive Total zcta_cum.perc_pos
##      <dbl>    <dbl> <dbl>             <dbl>
##  1      NA     1864  2078              89.7
##  2   10001      260   571              45.5
##  3   10002      712  1358              52.4
##  4   10003      347   830              41.8
##  5   10004       24    64              37.5
##  6   10005       44   137              32.1
##  7   10006       14    54              25.9
##  8   10007       40   130              30.8
##  9   10009      518  1180              43.9
## 10   10010      201   561              35.8
## # ℹ 168 more rows
nyc_zip_sf <- nyc_zip_sf %>%
  mutate(ZIPCODE = as.character(ZIPCODE))

covid_df <- covid_df %>%
  mutate(MODZCTA = as.character(MODZCTA))

zpNYC <- left_join(nyc_zip_sf, covid_df, by = c("ZIPCODE" = "MODZCTA"))

print(zpNYC)
## Simple feature collection with 263 features and 15 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)
## First 10 features:
##    ZIPCODE BLDGZIP  PO_NAME POPULATION     AREA STATE COUNTY ST_FIPS CTY_FIPS
## 1    11436       0  Jamaica      18681 22699295    NY Queens      36      081
## 2    11213       0 Brooklyn      62426 29631004    NY  Kings      36      047
## 3    11212       0 Brooklyn      83866 41972104    NY  Kings      36      047
## 4    11225       0 Brooklyn      56527 23698630    NY  Kings      36      047
## 5    11218       0 Brooklyn      72280 36868799    NY  Kings      36      047
## 6    11226       0 Brooklyn     106132 39408598    NY  Kings      36      047
## 7    11219       0 Brooklyn      92561 42002738    NY  Kings      36      047
## 8    11210       0 Brooklyn      67067 47887023    NY  Kings      36      047
## 9    11230       0 Brooklyn      80857 49926703    NY  Kings      36      047
## 10   11204       0 Brooklyn      77354 43555185    NY  Kings      36      047
##                     URL SHAPE_AREA SHAPE_LEN Positive Total zcta_cum.perc_pos
## 1  http://www.usps.com/          0         0      342   567             60.32
## 2  http://www.usps.com/          0         0      972  1653             58.80
## 3  http://www.usps.com/          0         0     1086  1793             60.57
## 4  http://www.usps.com/          0         0      814  1359             59.90
## 5  http://www.usps.com/          0         0     1163  1967             59.13
## 6  http://www.usps.com/          0         0     1336  2170             61.57
## 7  http://www.usps.com/          0         0     1961  3018             64.98
## 8  http://www.usps.com/          0         0     1162  1931             60.18
## 9  http://www.usps.com/          0         0     1573  2615             60.15
## 10 http://www.usps.com/          0         0     1325  2234             59.31
##                              geom
## 1  POLYGON ((1038098 188138.4,...
## 2  POLYGON ((1001614 186926.4,...
## 3  POLYGON ((1011174 183696.3,...
## 4  POLYGON ((995908.4 183617.6...
## 5  POLYGON ((991997.1 176307.5...
## 6  POLYGON ((994821.5 177865.7...
## 7  POLYGON ((987286.4 173946.5...
## 8  POLYGON ((995796 171110.1, ...
## 9  POLYGON ((994099.3 171240.7...
## 10 POLYGON ((989500.2 170730.2...
mapview(zpNYC, zcol = "Positive", layer.name = "COVID Positive Cases")

Task 3: Aggregate Retail Food Stores to Zip Code Areas

retail_sf <- st_transform(retail_sf, st_crs(zpNYC))

zpNYC <- retail_sf %>%
  dplyr::filter(stringr::str_detect(Establishment.Type, '[AJD]')) %>%
  sf::st_join(zpNYC, ., join = st_contains) %>%
  group_by(ZIPCODE) %>%
  summarise(FoodStoreNum = n())

print(zpNYC)
## Simple feature collection with 248 features and 2 fields
## Geometry type: GEOMETRY
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
## # A tibble: 248 × 3
##    ZIPCODE FoodStoreNum                                                     geom
##    <chr>          <int>                              <GEOMETRY [US_survey_foot]>
##  1 00083              1 POLYGON ((998309.7 229616.7, 998282.9 229566.7, 998251.…
##  2 10001             54 POLYGON ((981958.6 213464.5, 981980.3 213539.5, 981988.…
##  3 10002            199 POLYGON ((991339.9 207576.8, 991330.5 207541.4, 991328.…
##  4 10003             94 POLYGON ((989830.5 207048.1, 989716.4 206842.5, 989595.…
##  5 10004             13 MULTIPOLYGON (((977493.2 188450.6, 977481.3 188451.7, 9…
##  6 10005              9 POLYGON ((982595.7 195880.8, 982587.4 195873.8, 982500 …
##  7 10006              5 POLYGON ((981136.3 198082.4, 981481.1 197877.9, 981327.…
##  8 10007             20 POLYGON ((980538.9 200658.5, 980675.7 200568.3, 980754.…
##  9 10009             84 POLYGON ((991606.4 205494.1, 991619.9 205478.7, 991643.…
## 10 10010             48 POLYGON ((987500 210146.4, 987521.6 210135.9, 987535.4 …
## # ℹ 238 more rows
mapview(zpNYC, zcol = "FoodStoreNum", layer.name = "Number of Food Stores")

Task 4: Aggregate Nursing Homes to Zip Code Areas

nycNursingHome <- health_sf %>%
  dplyr::filter(Short.Description == "NH")

cat("Number of nursing homes:", nrow(nycNursingHome), "\n")
## Number of nursing homes: 620
nycNursingHome <- st_transform(nycNursingHome, st_crs(zpNYC))

zpNYC <- nycNursingHome %>%
  sf::st_join(zpNYC, ., join = st_contains) %>%
  group_by(ZIPCODE) %>%
  summarise(
    FoodStoreNum   = max(FoodStoreNum, na.rm = TRUE),
    NursingHomeNum = n()
  )

print(zpNYC)
## Simple feature collection with 248 features and 3 fields
## Geometry type: GEOMETRY
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
## # A tibble: 248 × 4
##    ZIPCODE FoodStoreNum NursingHomeNum                                      geom
##    <chr>          <int>          <int>               <GEOMETRY [US_survey_foot]>
##  1 00083              1              1 POLYGON ((998309.7 229616.7, 998282.9 22…
##  2 10001             54              1 POLYGON ((981958.6 213464.5, 981980.3 21…
##  3 10002            199              2 POLYGON ((991339.9 207576.8, 991330.5 20…
##  4 10003             94              1 POLYGON ((989830.5 207048.1, 989716.4 20…
##  5 10004             13              1 MULTIPOLYGON (((977493.2 188450.6, 97748…
##  6 10005              9              1 POLYGON ((982595.7 195880.8, 982587.4 19…
##  7 10006              5              1 POLYGON ((981136.3 198082.4, 981481.1 19…
##  8 10007             20              1 POLYGON ((980538.9 200658.5, 980675.7 20…
##  9 10009             84              1 POLYGON ((991606.4 205494.1, 991619.9 20…
## 10 10010             48              1 POLYGON ((987500 210146.4, 987521.6 2101…
## # ℹ 238 more rows
mapview(zpNYC, zcol = "NursingHomeNum", layer.name = "Number of Nursing Homes")

Task 5: Join ACS Census Data to Census Tracts and Aggregate to Zip Codes

nycCensus <- sf::st_read("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/elinorgoldsmith-greenberg/Documents/GTECH 385 Assignments/R-Spatial/data/Section_08/R-Spatial_II_Lab/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)
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("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,
    malePop     = DP05_0002E,
    femalePop   = DP05_0003E,
    whitePop    = DP05_0037E,
    blackPop    = DP05_0038E,
    asianPop    = DP05_0067E,
    hispanicPop = DP05_0071E
  ) %>%
  dplyr::mutate(censusCode = stringr::str_sub(GEO_ID, -9, -1))

popData <- merge(nycCensus, acsData, by.x = "tractFIPS", by.y = "censusCode")

cat("Total population:", sum(popData$totPop), "\n")
## Total population: 8443713
popNYC <- sf::st_transform(popData, st_crs(zpNYC))

covidPopZipNYC <- sf::st_join(zpNYC,
                               popNYC %>% sf::st_centroid(),
                               join = st_contains) %>%
  group_by(ZIPCODE, FoodStoreNum, NursingHomeNum) %>%
  summarise(
    totPop      = sum(totPop,      na.rm = TRUE),
    elderlyPop  = sum(elderlyPop,  na.rm = TRUE),
    whitePop    = sum(whitePop,    na.rm = TRUE),
    blackPop    = sum(blackPop,    na.rm = TRUE),
    asianPop    = sum(asianPop,    na.rm = TRUE),
    hispanicPop = sum(hispanicPop, na.rm = TRUE)
  )
## Warning: st_centroid assumes attributes are constant over geometries
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by ZIPCODE, FoodStoreNum, and NursingHomeNum.
## ℹ Output is grouped by ZIPCODE and FoodStoreNum.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(ZIPCODE, FoodStoreNum, NursingHomeNum))` for
##   per-operation grouping (`?dplyr::dplyr_by`) instead.
cat("Final rows:", nrow(covidPopZipNYC), "\n")
## Final rows: 248
print(covidPopZipNYC)
## Simple feature collection with 248 features and 9 fields
## Geometry type: GEOMETRY
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
## # A tibble: 248 × 10
## # Groups:   ZIPCODE, FoodStoreNum [248]
##    ZIPCODE FoodStoreNum NursingHomeNum totPop elderlyPop whitePop blackPop
##    <chr>          <int>          <int>  <int>      <int>    <int>    <int>
##  1 00083              1              1      3          0        1        2
##  2 10001             54              1  19146       2500    12485     1092
##  3 10002            199              2  74310      15815    24033     5969
##  4 10003             94              1  53487       6296    40503     3130
##  5 10004             13              1   1731         52     1207       71
##  6 10005              9              1   8809        209     6419      185
##  7 10006              5              1   4639         66     3130       52
##  8 10007             20              1  15510       2134     7912     1008
##  9 10009             84              1  58683       8907    35952     5745
## 10 10010             48              1  28129       3469    21438     1159
## # ℹ 238 more rows
## # ℹ 3 more variables: asianPop <int>, hispanicPop <int>,
## #   geom <GEOMETRY [US_survey_foot]>

Task 6: Verify and Save Final sf Object

mapview(covidPopZipNYC, zcol = "totPop",    layer.name = "Total Population") +
mapview(covidPopZipNYC, zcol = "elderlyPop", layer.name = "Elderly Population")
st_write(covidPopZipNYC,
         dsn = "lab_08_data.gpkg",
         layer = "nyc_zip_covid_final",
         delete_layer = TRUE)
## Deleting layer `nyc_zip_covid_final' using driver `GPKG'
## Writing layer `nyc_zip_covid_final' to data source 
##   `lab_08_data.gpkg' using driver `GPKG'
## Writing 248 features with 9 fields and geometry type Unknown (any).
cat("Final sf object saved to lab_08_data.gpkg\n")
## Final sf object saved to lab_08_data.gpkg