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.

task 1:

invisible(lapply(c("sf", "sp", "terra", "dplyr", "classInt", "RColorBrewer", "ggplot2", "ggmap", "tmap", "mapview", "leaflet"), function(pkg) {
  if (!require(pkg, character.only = TRUE)) install.packages(pkg, repos = "https://cloud.r-project.org")
  library(pkg, character.only = TRUE)
}))
## Loading required package: sp
## Loading required package: terra
## terra 1.9.1
## 
## Attaching package: 'terra'
## The following objects are masked from 'package:magrittr':
## 
##     extract, inset
## The following object is masked from 'package:tidyr':
## 
##     extract
## Loading required package: classInt
## Loading required package: RColorBrewer
## Loading required package: ggmap
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
##   Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service>
##   OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
## 
## Attaching package: 'ggmap'
## 
## 
## The following object is masked from 'package:terra':
## 
##     inset
## 
## 
## The following object is masked from 'package:magrittr':
## 
##     inset
## 
## 
## Loading required package: tmap
## 
## Loading required package: leaflet

task 2:

nyc_zipcodes <- st_read("D:/adult shit/Career/Masters/S2026/GTECH785/R_Spatial/Week_07/Section_07/R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `D:\adult shit\Career\Masters\S2026\GTECH785\R_Spatial\Week_07\Section_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)

task 3:

#Reading in NYS_Health_Facility.csv
NYS_Health_Facility <- read_csv("D:/adult shit/Career/Masters/S2026/GTECH785/R_Spatial/Week_07/Section_07/R-Spatial_I_Lab/NYS_Health_Facility.csv", 
                                 col_names = TRUE)
## 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.
#Converting to sf object, filtering for NAs
NYS_Health_Facility_sf <- NYS_Health_Facility %>%
  filter(!is.na(`Facility Longitude`) & !is.na(`Facility Latitude`)) %>%
  st_as_sf(coords = c("Facility Longitude", "Facility Latitude"))

#Filtering for NYC counties only
NYC_Health_Facility_sf <- NYS_Health_Facility_sf %>%
     filter(`Facility County` %in% c("Bronx", "Kings", "Richmond", "New York", "Queens"))

task 4:

#Read in data
nys_retail_food_store_xy <- read_csv(
  "D:/adult shit/Career/Masters/S2026/GTECH785/R_Spatial/Week_07/Section_07/R-Spatial_I_Lab/nys_retail_food_store_xy.csv",
  col_names = TRUE,
  locale = locale(encoding = "latin1")
)
## Rows: 29389 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): ï..County, Operation.Type, Establishment.Type, Entity.Name, DBA.Na...
## 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.
#Force rename first column
colnames(nys_retail_food_store_xy)[1] <- "County"

#Remove NAs
nys_retail_food_store_xy <- nys_retail_food_store_xy %>%
  filter(!is.na(X) & !is.na(Y))

#Convert to sf
nys_retail_food_store_xy_sf <- st_as_sf(nys_retail_food_store_xy, coords = c("X", "Y"))

#Filter for NYC counties
nyc_retail_food_store_xy_sf <- nys_retail_food_store_xy_sf %>%
  filter(County %in% c("Bronx", "Kings", "Richmond", "New York", "Queens"))

task 5:

#Filtering for NYC Facilities only (random data point appeared outside NYC)
NYC_Health_Facility_sf <- NYC_Health_Facility_sf %>%
    filter(
      st_coordinates(.)[, "X"] >= -74.3 & st_coordinates(.)[, "X"] <= -73.7 &
      st_coordinates(.)[, "Y"] >= 40.4  & st_coordinates(.)[, "Y"] <= 40.9)

#Visualization of all three layers together with mapview
mapview(nyc_zipcodes, layer.name = "NYC Zip Codes", col.regions = "lightblue", alpha.regions = 0.5) +
mapview(NYC_Health_Facility_sf, layer.name = "Health Facilities", col.regions = "red", cex = 3) +
mapview(nyc_retail_food_store_xy_sf, layer.name = "Retail Food Stores", col.regions = "green", cex = 3)

task 6:

#Save path
save(nyc_zipcodes, NYC_Health_Facility_sf, nys_retail_food_store_xy_sf, file = "D:/adult shit/Career/Masters/S2026/GTECH785/R_Spatial/Week_07/Section_07/R-Spatial_I_Lab/CK_Week07_Assignment.rmd")