R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Load Packages

library(sf)
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(mapview)

Read NYC Postal Area Shapefile

nyc_zip <- st_read("section_07/zip_code_040114/ZIP_CODE_040114.shp", quiet = T) |>
  st_transform(4326)

names(nyc_zip)
##  [1] "ZIPCODE"    "BLDGZIP"    "PO_NAME"    "POPULATION" "AREA"      
##  [6] "STATE"      "COUNTY"     "ST_FIPS"    "CTY_FIPS"   "URL"       
## [11] "SHAPE_AREA" "SHAPE_LEN"  "geometry"

##Process NYS Health Facilities Data

health_raw <- read_csv("section_07/NYS_Health_Facility.csv", show_col_types = F) |>
  clean_names()

names(health_raw)
##  [1] "facility_id"                  "facility_name"               
##  [3] "short_description"            "description"                 
##  [5] "facility_open_date"           "facility_address_1"          
##  [7] "facility_address_2"           "facility_city"               
##  [9] "facility_state"               "facility_zip_code"           
## [11] "facility_phone_number"        "facility_fax_number"         
## [13] "facility_website"             "facility_county_code"        
## [15] "facility_county"              "regional_office_id"          
## [17] "regional_office"              "main_site_name"              
## [19] "main_site_facility_id"        "operating_certificate_number"
## [21] "operator_name"                "operator_address_1"          
## [23] "operator_address_2"           "operator_city"               
## [25] "operator_state"               "operator_zip_code"           
## [27] "cooperator_name"              "cooperator_address"          
## [29] "cooperator_address_2"         "cooperator_city"             
## [31] "cooperator_state"             "cooperator_zip_code"         
## [33] "ownership_type"               "facility_latitude"           
## [35] "facility_longitude"           "facility_location"
health_sf <- health_raw |>
  filter(!is.na(facility_longitude), !is.na(facility_latitude)) |>
  mutate(
    longitude = as.numeric(facility_longitude),
    latitude = as.numeric(facility_latitude)
  ) |>
  st_as_sf(coords = c("longitude", "latitude"), crs = 4326, remove = F)

health_nyc_sf <- health_sf[
  st_within(health_sf, st_union(nyc_zip), sparse = F),
]

##Process NYS Retail Food Store Data

food_raw <- read_csv("section_07/nys_retail_food_store_xy.csv", show_col_types = F) |>
  clean_names()
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
## Warning in grepl(x = string, pattern = current_unicode, fixed = TRUE): input
## string 1 is invalid UTF-8
names(food_raw)
##  [1] "county"             "license_number"     "operation_type"    
##  [4] "establishment_type" "entity_name"        "dba_name"          
##  [7] "street_number"      "street_name"        "address_line_2"    
## [10] "address_line_3"     "city"               "state"             
## [13] "zip_code"           "square_footage"     "location"          
## [16] "coords"             "y"                  "x"
food_sf <- food_raw |>
  filter(!is.na(x), !is.na(y)) |>
  mutate(
    longitude = as.numeric(x),
    latitude = as.numeric(y)
  ) |>
  st_as_sf(coords = c("longitude", "latitude"), crs = 4326, remove = F)

food_nyc_sf <- food_sf[
  st_within(food_sf, st_union(nyc_zip), sparse = F),
]

##Verify Spatial Data with MapView

mapview(nyc_zip, alpha.regions = 0, lwd = 1) +
  mapview(health_nyc_sf, col.regions = "red", layer.name = "Health Facilities") +
  mapview(food_nyc_sf, col.regions = "blue", layer.name = "Food Stores")

##Save Spatial Objects

save(nyc_zip, health_nyc_sf, food_nyc_sf,
     file = "section_07/lab1_spatial_objects.RData")

st_write(nyc_zip, "section_07/lab1_spatial_data.gpkg",
         layer = "nyc_zip", delete_layer = T, quiet = T)
st_write(health_nyc_sf, "section_07/lab1_spatial_data.gpkg",
         layer = "health_facilities", delete_layer = T, quiet = T)
st_write(food_nyc_sf, "section_07/lab1_spatial_data.gpkg",
         layer = "food_stores", delete_layer = T, quiet = T)