Author

Alexandra Mejia

Published

March 11, 2026

Explanation of the template

Update the title with your information. Make sure to include identification information so that we know it is your submission.

Also update the author name and date accordingly.

Check out the Source Code from the top-right corner </>Code menu.

In the following R code chunk, load_packages is the code chunk name. include=FALSE suggests that the code chunk will run, but the code itself and its outputs will not be included in the rendered HTML. echo=TRUE in the following code chunk suggests that the code and results from running the code will be included in the rendered HTML.

R Spatial Lab Assignment # 1

Don’t use a single chunk for the entire assignment. Break it into multiple chunks.

You can name the code chunk and also set options.

task 1:

Show the code
smpCode <- "hello, R markdown and RPubs!"

cat(smpCode)
hello, R markdown and RPubs!

task 2:

Quarto markdown is different from R markdown in terms of chunk options. See chunk options at Quarto website.

Show the code
print("This is the new code chunk options available in Quarto Markdown")
[1] "This is the new code chunk options available in Quarto Markdown"

Lab Assignment

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.

Show the code
library(sf)
zip_code <- st_read("R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
Reading layer `ZIP_CODE_040114' from data source 
  `/Users/Ally/Desktop/R_Spatial_Section/Week_07/R-Spatial_I_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)

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

Show the code
nys_health <- read_csv("R-Spatial_I_Lab/NYS_Health_Facility.csv",
                       show_col_types = FALSE)

nys_health_clean <- nys_health %>%
  filter(
    !is.na(`Facility Longitude`),
    !is.na(`Facility Latitude`),
    `Facility Longitude` != 0,
    `Facility Latitude` != 0
  )

nys_health_sf <- st_as_sf(nys_health_clean, coords = c("Facility Longitude", "Facility Latitude"))

str(nys_health_sf)
sf [3,844 × 35] (S3: sf/tbl_df/tbl/data.frame)
 $ Facility ID                 : num [1:3844] 204 620 1156 2589 3455 ...
 $ Facility Name               : chr [1:3844] "Hospice at Lourdes" "Charles T Sitrin Health Care Center Inc" "East Side Nursing Home" "Wellsville Manor Care Center" ...
 $ Short Description           : chr [1:3844] "HSPC" "NH" "NH" "NH" ...
 $ Description                 : chr [1:3844] "Hospice" "Residential Health Care Facility - SNF" "Residential Health Care Facility - SNF" "Residential Health Care Facility - SNF" ...
 $ Facility Open Date          : chr [1:3844] "06/01/1985" "02/01/1989" "08/01/1979" "02/01/1989" ...
 $ Facility Address 1          : chr [1:3844] "4102 Old Vestal Road" "2050 Tilden Avenue" "62 Prospect St" "4192A Bolivar Road" ...
 $ Facility Address 2          : chr [1:3844] NA NA NA NA ...
 $ Facility City               : chr [1:3844] "Vestal" "New Hartford" "Warsaw" "Wellsville" ...
 $ Facility State              : chr [1:3844] "New York" "New York" "New York" "New York" ...
 $ Facility Zip Code           : chr [1:3844] "13850" "13413" "14569" "14895" ...
 $ Facility Phone Number       : num [1:3844] 6.08e+09 3.16e+09 5.86e+09 5.86e+09 7.17e+09 ...
 $ Facility Fax Number         : num [1:3844] NA NA NA NA NA ...
 $ Facility Website            : chr [1:3844] NA NA NA NA ...
 $ Facility County Code        : num [1:3844] 3 32 60 2 14 ...
 $ Facility County             : chr [1:3844] "Broome" "Oneida" "Wyoming" "Allegany" ...
 $ Regional Office ID          : num [1:3844] 3 3 1 1 1 7 1 7 5 7 ...
 $ Regional Office             : chr [1:3844] "Central New York Regional Office" "Central New York Regional Office" "Western Regional Office - Buffalo" "Western Regional Office - Buffalo" ...
 $ Main Site Name              : chr [1:3844] NA NA NA NA ...
 $ Main Site Facility ID       : num [1:3844] NA NA NA NA NA ...
 $ Operating Certificate Number: chr [1:3844] "0301501F" "3227304N" "6027303N" "0228305N" ...
 $ Operator Name               : chr [1:3844] "Our Lady of Lourdes Memorial Hospital Inc" "Charles T Sitrin Health Care Center, Inc" "East Side Nursing Home Inc" "Wellsville Manor LLC" ...
 $ Operator Address 1          : chr [1:3844] "169 Riverside Drive" "Box 1000 Tilden Avenue" "62 Prospect Street" "4192a Bolivar Road" ...
 $ Operator Address 2          : chr [1:3844] NA NA NA NA ...
 $ Operator City               : chr [1:3844] "Binghamton" "New Hartford" "Warsaw" "Wellsville" ...
 $ Operator State              : chr [1:3844] "New York" "New York" "New York" "New York" ...
 $ Operator Zip Code           : chr [1:3844] "13905" "13413" "14569" "14897" ...
 $ Cooperator Name             : chr [1:3844] NA NA NA NA ...
 $ Cooperator Address          : chr [1:3844] NA NA NA NA ...
 $ Cooperator Address 2        : chr [1:3844] NA NA NA NA ...
 $ Cooperator City             : chr [1:3844] NA NA NA NA ...
 $ Cooperator State            : chr [1:3844] "New York" "New York" "New York" "New York" ...
 $ Cooperator Zip Code         : chr [1:3844] NA NA NA NA ...
 $ Ownership Type              : chr [1:3844] "Not for Profit Corporation" "Not for Profit Corporation" "Business Corporation" "LLC" ...
 $ Facility Location           : chr [1:3844] "(42.097095, -75.975243)" "(43.05497, -75.228828)" "(42.738979, -78.12867)" "(42.126461, -77.967834)" ...
 $ geometry                    :sfc_POINT of length 3844; first list element:  'XY' num [1:2] -76 42.1
 - 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:34] "Facility ID" "Facility Name" "Short Description" "Description" ...

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

Show the code
nys_food_store <- read_csv("R-Spatial_I_Lab/nys_retail_food_store_xy.csv",
                           locale = locale(encoding = "Latin1"),
                           show_col_types = FALSE)

nys_food_store_clean <- nys_food_store %>%
  filter(
    !is.na(`X`),
    !is.na(`Y`),
    `X` != 0,
    `Y` != 0,
    `ï..County` %in% c("Bronx", "Kings", "New York", "Queens", "Richmond")
  )

nys_food_store_sf <- st_as_sf(nys_food_store_clean, coords = c("X", "Y"))

str(nys_food_store_sf)
sf [11,300 × 17] (S3: sf/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" ...
 $ geometry          :sfc_POINT of length 11300; first list element:  'XY' num [1:2] -73.8 40.9
 - 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:16] "ï..County" "License.Number" "Operation.Type" "Establishment.Type" ...
  1. Use simple mapping method such as mapview with a basemap to verify the above datasets in terms of their geographic locations.

Map of NYS health facilities – zoomed in to exclude a misplaced outlier point

Show the code
plot(st_geometry(nys_health_sf), pch = 21, bg = "pink", col = "black",
     xlim = c(-80, -71), ylim = c(40.3, 45.2))

Map for NYS retail food stores

Show the code
plot(st_geometry(nys_food_store_sf), pch = 21, bg = "purple", col = "black")

  1. Save the three sf objects in a RData file or in a single GeoPackage file/database.
Show the code
save(zip_code, nys_health_sf, nys_food_store_sf,  
    file = "sf_objects.RData")