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)
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.
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.
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")
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.
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.
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.
Source Code
---title: "R Week 7 Assignment"author: "Antonio Osso"date: "3/13/2026"format: html: toc: true toc-location: left code-fold: true code-tools: true---```{r setup, include=FALSE}knitr::opts_chunk$set(echo =TRUE)```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 load_packages, include=FALSE}require(tidyverse);require(sf); require(mapview); require(magrittr)```## R Spatial Lab Assignment \# 1Don'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```{r task1, echo=TRUE}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")```### Task 2```{r task2, echo=TRUE}health_fac <-read_csv("R-Spatial_I_Lab/NYS_Health_Facility.csv")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)```### Task 3```{r task3, echo=TRUE}food_stores <-read_csv("R-Spatial_I_Lab/nys_retail_food_store_xy.csv")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)```### Task 4```{r task4, echo=TRUE}#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```{r task5, echo=TRUE}gpkg_path <-"nyc_spatial.gpkg"st_write(nyc_postal, gpkg_path, layer ="nyc_postal", delete_layer =TRUE)st_write(health_fac_sf, gpkg_path, layer ="health_fac", delete_layer =TRUE)st_write(food_stores_nyc, gpkg_path, layer ="food_stores", delete_layer =TRUE)```