2. Read the NYC postal areas in Shapefiles into sf objects. As NYC DOH publishes COVID-19 data by zip code, we will utilize the postal area data later.

nyc_sf <- st_read("data/nyc/nyc_acs_tracts.shp")
## Reading layer `nyc_acs_tracts' from data source 
##   `H:\Hunter Class\GISr\Section_07\data\nyc\nyc_acs_tracts.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2166 features and 113 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
## Geodetic CRS:  NAD83
nyc_sf
## Simple feature collection with 2166 features and 113 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
## Geodetic CRS:  NAD83
## First 10 features:
##    UNEMP_RATE cartodb_id withssi withsocial withpubass struggling profession
## 1   0.0000000          1       0          0          0          0          0
## 2   0.0817052          2     228        353         47        694          0
## 3   0.1706029          3     658       1577        198       2589         36
## 4   0.0000000          4       0          0          0          0          0
## 5   0.0880372          5     736       1382        194       2953         19
## 6   0.0562960          6       0         99          0        337        745
## 7   0.0844740          7     261       1122        145       3085         18
## 8   0.0395738          8       0         10          0         17         49
## 9   0.1442177          9      39        264          5        131         35
## 10  0.2163820         10     638        895        334       1938          0
##    popunemplo poptot popover18 popinlabou poororstru poor pacificune pacificinl
## 1           0      0         0          0          0    0          0          0
## 2          92   2773      2351       1126       2026 1332          0          0
## 3         549   8339      6878       3218       4833 2244          0          0
## 4           0      0         0          0          0    0          0          0
## 5         379  10760      8867       4305       7044 4091         13         13
## 6         321   7024      6637       5702       1041  704          0          0
## 7         432  10955      8932       5114       6376 3291          0          0
## 8          26    849       711        657        112   95          0          0
## 9         106   1701      1241        735        232  101          0          0
## 10        494   5923      4755       2283       4115 2177          0          0
##    pacific otherunemp otherinlab otherethni onlyprofes onlymaster onlylessth
## 1        0          0          0          0          0          0          0
## 2        0          0        144        598          0         77        878
## 3        0        103        348       1175        102        412       2164
## 4        0          0          0          0          0          0          0
## 5       13         46        609       1799         20        292       3793
## 6        0          0        112        112        890       2162        121
## 7        0          0        184        377        116        408       4384
## 8        0          0         48         48         78        233         34
## 9        0         17         90        183         77        328        115
## 10       0        151        525       1664          0         16       1846
##    onlyhighsc onlydoctor onlycolleg onlybachel okay mixedunemp mixedinlab mixed
## 1           0          0          0          0    0          0          0     0
## 2        1088          0        471        271  742         16         72   175
## 3        4068         66       2355       1269 3474         21        134   234
## 4           0          0          0          0    0          0          0     0
## 5        3868          1       2290       1322 3499         14        100   251
## 6        5508        145       5371       4685 5982         24        136   224
## 7        3882         98       2223       1262 4579          0         91   115
## 8         631         29        585        481  733          0         10    16
## 9        1093         42        884        687 1469         51         62   102
## 10       2343          0       1111        200 1798         31        140   334
##    master maleunempl maleover18 malepro malemastr male_lesHS male_HS male_doctr
## 1       0          0          0       0         0          0       0          0
## 2      77         76       1101       0        10        302     607          0
## 3     310        349       3134      36       143       1063    1893         24
## 4       0          0          0       0         0          0       0          0
## 5     272        179       4068      19       126       1693    1651          0
## 6    1272        204       3557     473      1034         29    2985         77
## 7     292        197       3992      48       181       2013    1702         30
## 8     155          8        454      51       123         17     414         29
## 9     251         72        613      61       139        103     485         38
## 10     16        277       1935       0         0        714     985          0
##    male_collg male_BA maleinlabo maledrop male16to19 male lessthanhi lessthan10
## 1           0       0          0        0          0    0          0          0
## 2         227     132        612       16         65 1318        878        212
## 3        1225     668       1640        8        253 3850       2164        760
## 4           0       0          0        0          0    0          0          0
## 5         997     562       2059        0        162 4796       3793       1100
## 6        2924    2411       3299        0         37 3840        121        289
## 7         871     499       2466        0        236 5244       4384        625
## 8         381     288        423        0         20  533         34          2
## 9         368     292        392        0         32  897        115         51
## 10        462      43        991        0        187 2514       1846        463
##    households hispanicun hispanicin hispanic highschool geo_state geo_place
## 1           0          0          0        0          0        36     51000
## 2         986         30        357     1187        617        36     51000
## 3        3382        269       1171     3503       1713        36     51000
## 4           0          0          0        0          0        36     51000
## 5        3838        115        819     2608       1578        36     51000
## 6        4104         27        297      374        137        36     51000
## 7        3950          0        236      318       1659        36     51000
## 8         367          0         77      108         46        36     51000
## 9         739         68        201      360        209        36     51000
## 10       2224        335       1145     3478       1232        36     51000
##    geo_county field_1 femaleunem femaleover fem_profes fem_master fem_lessHS
## 1          61       1          0          0          0          0          0
## 2          61       2         16       1250          0         67        576
## 3          61       3        200       3744         66        269       1101
## 4          61       4          0          0          0          0          0
## 5          61       5        200       4799          1        166       2100
## 6          61       6        117       3080        417       1128         92
## 7          61       7        235       4940         68        227       2371
## 8          61       8         18        257         27        110         17
## 9          61       9         34        628         16        189         12
## 10         61      10        217       2820          0         16       1132
##    fem_HS fem_doctor fem_colleg fem_BA femaleinla femaledrop femal16_19 female
## 1       0          0          0      0          0          0          0      0
## 2     481          0        244    139        514          0         84   1455
## 3    2175         42       1130    601       1578          1        124   4489
## 4       0          0          0      0          0          0          0      0
## 5    2217          1       1293    760       2246          0        271   5964
## 6    2523         68       2447   2274       2403          0          2   3184
## 7    2180         68       1352    763       2648          0        126   5711
## 8     217          0        204    193        234          0          2    316
## 9     608          4        516    395        343          0         32    804
## 10   1358          0        649    157       1292         14        242   3409
##    europeanun europeanin european doctorate com_90plus comm_5less comm_60_89
## 1           0          0        0         0          0          0          0
## 2          14        303      540         0         49         99         40
## 3         328       1641     4091        66        101          0        215
## 4           0          0        0         0          0          0          0
## 5          56        525     1181         1         36          1        370
## 6         188       4058     4975       145         61        220        136
## 7          71        492      929        98         82         46        687
## 8          26        540      717        29         17         26         32
## 9          22        543     1327        42         27          0         41
## 10        132        510     1645         0        178          0        331
##    comm_5_14 comm_45_59 comm_30_44 comm_15_29 college bachelor asianunemp
## 1          0          0          0          0       0        0          0
## 2        121        142        217        352     200      194         62
## 3        226        171        923        970    1086      857         38
## 4          0          0          0          0       0        0          0
## 5        331        284       1442       1250     968     1030        207
## 6        913        287       1514       1840     686     2523        108
## 7        718        409       1118       1363     961      854        352
## 8         87         46        198        192     104      248          0
## 9         58         84        210        134     197      359         16
## 10        99        351        493        304     911      184         61
##    asianinlab asian americanun americanin american africanune africaninl
## 1           0     0          0          0        0          0          0
## 2         559  1249          0          0       43          0         48
## 3         615  1724          0          0        0         59        434
## 4           0     0          0          0        0          0          0
## 5        2736  6549          0         57       57         43        265
## 6        1286  1598          1          1        1          0        109
## 7        4283  9448          0          0        0          9         64
## 8          42    51          0          0        0          0         17
## 9          35    75          0          0        0          0          5
## 10        491   922         24         24       51         95        593
##    african puma                           ntaname ntacode ctlabel cdeligibil
## 1        0 3810       park-cemetery-etc-Manhattan    MN99       1          I
## 2      168 3809                   Lower East Side    MN28    2.01          E
## 3     1115 3809                   Lower East Side    MN28    2.02          E
## 4        0 3810       park-cemetery-etc-Manhattan    MN99       5          I
## 5      910 3809                   Lower East Side    MN28       6          E
## 6      114 3810 Battery Park City-Lower Manhattan    MN25       7          I
## 7       86 3809                         Chinatown    MN27       8          E
## 8       17 3810 Battery Park City-Lower Manhattan    MN25       9          I
## 9       14 3809                   Lower East Side    MN28   10.01          I
## 10    1307 3809                   Lower East Side    MN28   10.02          E
##     boroname medianinco medianagem medianagef medianage househol_1   gini
## 1  Manhattan         NA         NA         NA        NA         NA     NA
## 2  Manhattan      17282       39.3       43.8      43.0        2.8 0.4714
## 3  Manhattan      24371       44.9       47.9      45.7        2.4 0.5679
## 4  Manhattan         NA         NA         NA        NA         NA     NA
## 5  Manhattan      18832       43.4       43.0      43.2       2.74 0.5565
## 6  Manhattan     123516       29.7       28.3      29.0       1.71 0.4935
## 7  Manhattan      31196       42.0       43.0      42.2       2.77 0.4585
## 8  Manhattan     144018       33.2       31.9      32.9       2.17 0.4241
## 9  Manhattan      80223       41.5       45.7      43.1        2.3 0.5151
## 10 Manhattan      15890       31.2       47.5      39.3       2.55 0.4723
##    geo_stusab
## 1          ny
## 2          ny
## 3          ny
## 4          ny
## 5          ny
## 6          ny
## 7          ny
## 8          ny
## 9          ny
## 10         ny
##                                                                                  geo_name
## 1      Census Tract 1, New York city (part), Manhattan borough, New York County, New York
## 2   Census Tract 2.01, New York city (part), Manhattan borough, New York County, New York
## 3   Census Tract 2.02, New York city (part), Manhattan borough, New York County, New York
## 4      Census Tract 5, New York city (part), Manhattan borough, New York County, New York
## 5      Census Tract 6, New York city (part), Manhattan borough, New York County, New York
## 6      Census Tract 7, New York city (part), Manhattan borough, New York County, New York
## 7      Census Tract 8, New York city (part), Manhattan borough, New York County, New York
## 8      Census Tract 9, New York city (part), Manhattan borough, New York County, New York
## 9  Census Tract 10.01, New York city (part), Manhattan borough, New York County, New York
## 10 Census Tract 10.02, New York city (part), Manhattan borough, New York County, New York
##                       geo_geoid shape_leng shape_area ct2010 boroct2010
## 1  08000US360614491951000000100  11023.048  1844421.2    100    1000100
## 2  08000US360614491951000000201   4748.704   971628.2    201    1000201
## 3  08000US360614491951000000202   8568.208  3315121.2    202    1000202
## 4  08000US360614491951000000500  32407.899  9081808.1    500    1000500
## 5  08000US360614491951000000600   6973.204  2583270.4    600    1000600
## 6  08000US360614491951000000700  12076.323  2528569.6    700    1000700
## 7  08000US360614491951000000800   6353.031  2356896.9    800    1000800
## 8  08000US360614491951000000900  16484.099  3122058.8    900    1000900
## 9  08000US360614491951000001001   3924.837   867808.5   1001    1001001
## 10 08000US360614491951000001002   6315.120  2225016.1   1002    1001002
##    borocode tractid     popdty    geo_fips                       geometry
## 1         1    1.00      0.000 3.60614e+20 MULTIPOLYGON (((-74.04388 4...
## 2         1    2.01  79619.050 3.60614e+20 MULTIPOLYGON (((-73.9845 40...
## 3         1    2.02  72326.460 3.60614e+20 MULTIPOLYGON (((-73.98237 4...
## 4         1    5.00      0.000 3.60614e+20 MULTIPOLYGON (((-74.01093 4...
## 5         1    6.00 109785.000 3.60614e+20 MULTIPOLYGON (((-73.99022 4...
## 6         1    7.00  71641.800 3.60614e+20 MULTIPOLYGON (((-73.998 40....
## 7         1    8.00 130376.500 3.60614e+20 MULTIPOLYGON (((-73.99191 4...
## 8         1    9.00   6530.409 3.60614e+20 MULTIPOLYGON (((-74.00078 4...
## 9         1   10.01  55484.370 3.60614e+20 MULTIPOLYGON (((-73.97519 4...
## 10        1   10.02  76331.140 3.60614e+20 MULTIPOLYGON (((-73.97351 4...
nycPostal_sf <- st_read("data/ZIP/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `H:\Hunter Class\GISr\Section_07\data\ZIP\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)
str(nycPostal_sf)
## Classes 'sf' and 'data.frame':   263 obs. of  13 variables:
##  $ ZIPCODE   : chr  "11436" "11213" "11212" "11225" ...
##  $ BLDGZIP   : chr  "0" "0" "0" "0" ...
##  $ PO_NAME   : chr  "Jamaica" "Brooklyn" "Brooklyn" "Brooklyn" ...
##  $ POPULATION: num  18681 62426 83866 56527 72280 ...
##  $ AREA      : num  22699295 29631004 41972104 23698630 36868799 ...
##  $ STATE     : chr  "NY" "NY" "NY" "NY" ...
##  $ COUNTY    : chr  "Queens" "Kings" "Kings" "Kings" ...
##  $ ST_FIPS   : chr  "36" "36" "36" "36" ...
##  $ CTY_FIPS  : chr  "081" "047" "047" "047" ...
##  $ URL       : chr  "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" ...
##  $ SHAPE_AREA: num  0 0 0 0 0 0 0 0 0 0 ...
##  $ SHAPE_LEN : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ geometry  :sfc_POLYGON of length 263; first list element: List of 1
##   ..$ : num [1:159, 1:2] 1038098 1038142 1038171 1038280 1038521 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:12] "ZIPCODE" "BLDGZIP" "PO_NAME" "POPULATION" ...
plot(st_geometry(nycPostal_sf), main='NYC postal areas')

3. Read and process the NYS health facilities spreadsheet data. Create sf objects from geographic coordinates.

NYS_H_Facilites_df <- read_csv("data/NYS_Health_Facility.csv", 
                               show_col_types = FALSE, 
                               lazy = FALSE)

#Data Cleaning

NYS_H_Facilites_clean <- NYS_H_Facilites_df %>%
  mutate(
    `Facility Latitude` = if_else(`Facility ID` == 5764, 43.211623, `Facility Latitude`),
    `Facility Longitude` = if_else(`Facility ID` == 5764, -75.459349, `Facility Longitude`)
  ) %>%
  filter(complete.cases(NYS_H_Facilites_df[, c("Facility Longitude", "Facility Latitude")]), 
         `Facility Longitude` != 0,
         `Facility Latitude` != 0) %>%
  mutate(`Facility Latitude` = abs(`Facility Latitude`)) %>%
  mutate(`Facility Longitude` = abs(`Facility Longitude`) * -1)


NYC_H_Facilites_clean <- NYS_H_Facilites_clean %>%
  filter(
    `Facility County` == "Queens" | 
      `Facility County` == "Bronx" |
      `Facility County` == "Kings" |
      `Facility County` == "Richmond" |
      `Facility County` == "New York" 
  )

NYC_H_Facilites_sf <- st_as_sf(NYC_H_Facilites_clean, 
                               coords = c("Facility Longitude", "Facility Latitude"))

st_crs(NYC_H_Facilites_sf) <- 4326

4. Read and process the NYS retail food stores data. Create sf objects from geographic coordinates for NYC.

df <- read_csv("data/nys_retail_food_store_xy.csv", 
                        show_col_types = FALSE, 
                        lazy = FALSE)

NYS_Food_df <- df %>%
  rename(County = 1)

NYS_Food_clean <- NYS_Food_df %>%
  filter(complete.cases(NYS_Food_df [, c("Y", "X")]), 
         X != 0,
         Y != 0) %>%
  mutate(Y = abs(Y)) %>%
  mutate(X = abs(X) * -1)

NYC_Food_clean <- NYS_Food_clean %>%
  filter(
    County == "Queens" | 
    County == "Bronx" |
    County == "Kings" |
    County == "Richmond" |
    County == "New York" 
  )


str(NYC_Food_clean)
## tibble [11,300 × 18] (S3: tbl_df/tbl/data.frame)
##  $ County            : chr [1:11300] "Bronx" "Bronx" "Bronx" "Bronx" ...
##  $ License.Number    : num [1:11300] 734149 606221 606228 723375 724807 ...
##  $ Operation.Type    : chr [1:11300] "Store" "Store" "Store" "Store" ...
##  $ Establishment.Type: chr [1:11300] "JAC" "JAC" "JAC" "JAC" ...
##  $ Entity.Name       : chr [1:11300] "7 ELEVEN FOOD STORE #37933H" "1001 SAN MIGUEL FOOD CENTER INC" "1029 FOOD PLAZA INC" "1078 DELI GROCERY CORP" ...
##  $ DBA.Name          : chr [1:11300] NA "1001 SAN MIGUEL FD CNTR" "1029 FOOD PLAZA" "1078 DELI GROCERY" ...
##  $ Street.Number     : chr [1:11300] "500" "1001" "122" "1078" ...
##  $ Street.Name       : chr [1:11300] "BAYCHESTER AVE" "SHERIDAN AVE" "E 181ST ST" "EAST 165TH STREET" ...
##  $ Address.Line.2    : logi [1:11300] NA NA NA NA NA NA ...
##  $ Address.Line.3    : logi [1:11300] NA NA NA NA NA NA ...
##  $ City              : chr [1:11300] "BRONX" "BRONX" "BRONX" "BRONX" ...
##  $ State             : chr [1:11300] "NY" "NY" "NY" "NY" ...
##  $ Zip.Code          : num [1:11300] 10475 10456 10453 10459 10456 ...
##  $ Square.Footage    : num [1:11300] 0 1100 2000 1200 1500 2400 1000 1200 3400 500 ...
##  $ Location          : chr [1:11300] "500 BAYCHESTER AVE\r\nBRONX, NY 10475\r\n(40.869156, -73.831875)" "1001 SHERIDAN AVE\r\nBRONX, NY 10456\r\n(40.829061, -73.919613)" "122 E 181ST ST\r\nBRONX, NY 10453\r\n(40.854755, -73.902853)" "1078 EAST 165TH STREET\r\nBRONX, NY 10459\r\n(40.825105, -73.890589)" ...
##  $ Coords            : chr [1:11300] "40.869156, -73.831875" "40.829061, -73.919613" "40.854755, -73.902853" "40.825105, -73.890589" ...
##  $ Y                 : num [1:11300] 40.9 40.8 40.9 40.8 40.8 ...
##  $ X                 : num [1:11300] -73.8 -73.9 -73.9 -73.9 -73.9 ...
NYC_Food_sf <- st_as_sf(NYC_Food_clean, 
                               coords = c("X", "Y"))

st_crs(NYC_Food_sf) <- 4326

5. Use simple mapping method such as mapview with a basemap to verify the above datasets in terms of their geometry locations.

nycPostal_sf %>% dplyr::select(ZIPCODE, PO_NAME, POPULATION, COUNTY) %>%
  mapview::mapview()
NYC_H_Facilites_sf %>% dplyr::select(`Facility Name`, `Facility Zip Code`, Description, `Facility ID`, `Facility County`) %>%
  mapview::mapview()
NYC_Food_sf %>% 
  mapview::mapview()

6. Save the three sf objects in a RData file or in a single GeoPackage file/database.

st_write(nycPostal_sf, 
         dsn = './data/NYCData.gpkg', 
         layer='NYC Postal Area',
         delete_layer = TRUE)
## Deleting layer `NYC Postal Area' using driver `GPKG'
## Writing layer `NYC Postal Area' to data source 
##   `./data/NYCData.gpkg' using driver `GPKG'
## Writing 263 features with 12 fields and geometry type Polygon.
st_write(NYC_H_Facilites_sf, 
         dsn = './data/NYCData.gpkg', 
         layer='NYC Health Facilities',
         delete_layer = TRUE)
## Deleting layer `NYC Health Facilities' using driver `GPKG'
## Writing layer `NYC Health Facilities' to data source 
##   `./data/NYCData.gpkg' using driver `GPKG'
## Writing 1292 features with 34 fields and geometry type Point.
st_write(NYC_Food_sf, 
         dsn = './data/NYCData.gpkg', 
         layer='NYC Retail Food Stores',
         delete_layer = TRUE)
## Deleting layer `NYC Retail Food Stores' using driver `GPKG'
## Writing layer `NYC Retail Food Stores' to data source 
##   `./data/NYCData.gpkg' using driver `GPKG'
## Writing 11300 features with 16 fields and geometry type Point.