Author

Antonio Osso

Published

March 13, 2026

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

Code
if (!dir.exists("R-Spatial_I_Lab")) {
  unzip("R-Spatial_I_Lab.zip", exdir = "R-Spatial_I_Lab")
}

if (!dir.exists("R-Spatial_I_Lab/ZIP_CODE_040114")) {
  unzip("R-Spatial_I_Lab/ZIP_CODE_040114.zip", exdir = "R-Spatial_I_Lab/ZIP_CODE_040114")
}

nyc_postal <- st_read("R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
Reading layer `ZIP_CODE_040114' from data source 
  `C:\Users\anton\Documents\Week7_Lab\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)

Task 2

Code
health_fac <- read_csv("R-Spatial_I_Lab/NYS_Health_Facility.csv")
Rows: 3990 Columns: 36
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (28): Facility Name, Short Description, Description, Facility Open Date,...
dbl  (8): Facility ID, Facility Phone Number, Facility Fax Number, Facility ...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
health_fac_sf <- health_fac %>%
  filter(!is.na(`Facility Longitude`), !is.na(`Facility Latitude`)) %>%
  st_as_sf(coords = c("Facility Longitude", "Facility Latitude"), crs = 4326)

print(health_fac_sf)
Simple feature collection with 3848 features and 34 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -79.6299 ymin: -75.45935 xmax: 43.21162 ymax: 44.97849
Geodetic CRS:  WGS 84
# A tibble: 3,848 × 35
   `Facility ID` `Facility Name`                 `Short Description` Description
 *         <dbl> <chr>                           <chr>               <chr>      
 1           204 Hospice at Lourdes              HSPC                Hospice    
 2           620 Charles T Sitrin Health Care C… NH                  Residentia…
 3          1156 East Side Nursing Home          NH                  Residentia…
 4          2589 Wellsville Manor Care Center    NH                  Residentia…
 5          3455 Harris Hill Nursing Facility, … NH                  Residentia…
 6          3853 Garden City Surgi Center        DTC                 Diagnostic…
 7          4249 Willcare                        CHHA                Certified …
 8          4473 Good Shepherd Hospice           HSPC                Hospice    
 9          6230 NYU Langone Rutherford          HOSP-EC             Hospital E…
10          6482 Endoscopy Center of Long Islan… DTC                 Diagnostic…
# ℹ 3,838 more rows
# ℹ 31 more variables: `Facility Open Date` <chr>, `Facility Address 1` <chr>,
#   `Facility Address 2` <chr>, `Facility City` <chr>, `Facility State` <chr>,
#   `Facility Zip Code` <chr>, `Facility Phone Number` <dbl>,
#   `Facility Fax Number` <dbl>, `Facility Website` <chr>,
#   `Facility County Code` <dbl>, `Facility County` <chr>,
#   `Regional Office ID` <dbl>, `Regional Office` <chr>, …

Task 3

Code
food_stores <- read_csv("R-Spatial_I_Lab/nys_retail_food_store_xy.csv")
Rows: 29389 Columns: 18
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (11): County, Operation.Type, Establishment.Type, Entity.Name, DBA.Name,...
dbl  (4): License.Number, Zip.Code, Y, X
num  (1): Square.Footage
lgl  (2): Address.Line.2, Address.Line.3

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
nyc_counties <- c("New York", "Bronx", "Kings", "Queens", "Richmond")

food_stores_nyc <- food_stores %>%
  filter(County %in% nyc_counties) %>%
  filter(!is.na(X), !is.na(Y)) %>%
  st_as_sf(coords = c("X", "Y"), crs = 4326)

print(food_stores_nyc)
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
# A tibble: 11,300 × 17
   County License.Number Operation.Type Establishment.Type Entity.Name  DBA.Name
 * <chr>           <dbl> <chr>          <chr>              <chr>        <chr>   
 1 Bronx          734149 Store          JAC                7 ELEVEN FO… <NA>    
 2 Bronx          606221 Store          JAC                1001 SAN MI… 1001 SA…
 3 Bronx          606228 Store          JAC                1029 FOOD P… 1029 FO…
 4 Bronx          723375 Store          JAC                1078 DELI G… 1078 DE…
 5 Bronx          724807 Store          JAC                1086 LUNA D… 1086 LU…
 6 Bronx          712943 Store          JAC                109 AJ DELI… 109 AJ …
 7 Bronx          703060 Store          JAC                10 NEIGHBOR… 10 NEIG…
 8 Bronx          609065 Store          JAC                1105 TINTON… 1105 TI…
 9 Bronx          722972 Store          A                  1150 WEBSTE… 1150 WE…
10 Bronx          609621 Store          JAC                1158 GROCER… 1158 GR…
# ℹ 11,290 more rows
# ℹ 11 more variables: Street.Number <chr>, Street.Name <chr>,
#   Address.Line.2 <lgl>, Address.Line.3 <lgl>, City <chr>, State <chr>,
#   Zip.Code <dbl>, Square.Footage <dbl>, Location <chr>, Coords <chr>,
#   geometry <POINT [°]>

Task 4

Code
#It wouldn't render, so I've included the code and the image.
#mapview(nyc_postal) +
  #mapview(health_fac_sf, col.regions = "blue", cex = 3) +
  #mapview(food_stores_nyc, col.regions = "red", cex = 2)
knitr::include_graphics("Assignment7_Plot.png")

Task 5

Code
gpkg_path <- "nyc_spatial.gpkg"

st_write(nyc_postal,      gpkg_path, layer = "nyc_postal",  delete_layer = TRUE)
Deleting layer `nyc_postal' using driver `GPKG'
Writing layer `nyc_postal' to data source `nyc_spatial.gpkg' using driver `GPKG'
Writing 263 features with 12 fields and geometry type Polygon.
Code
st_write(health_fac_sf,   gpkg_path, layer = "health_fac",  delete_layer = TRUE)
Deleting layer `health_fac' using driver `GPKG'
Writing layer `health_fac' to data source `nyc_spatial.gpkg' using driver `GPKG'
Writing 3848 features with 34 fields and geometry type Point.
Code
st_write(food_stores_nyc, gpkg_path, layer = "food_stores",  delete_layer = TRUE)
Deleting layer `food_stores' using driver `GPKG'
Writing layer `food_stores' to data source `nyc_spatial.gpkg' using driver `GPKG'
Writing 11300 features with 16 fields and geometry type Point.