1. Set up project

2. Read NYC postal areas in Shapefile to sf objects

nyc_zip <- st_read("R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `/Users/jorgesoldevila/Desktop/OneDrive - Hunter - CUNY/R Data Analysis and Visualization/Section_10/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 NYC public health services spreadsheet data

health_services_df <- read_csv("./R-Spatial_I_Lab/NYS_Health_Facility.csv") %>%
  drop_na(`Facility Longitude`,
                        `Facility Latitude`) %>% filter(`Facility Longitude`!=0)
health_services_sf <- st_as_sf(health_services_df, 
                      coords = c("Facility Longitude", "Facility Latitude")) %>%
                      st_set_crs(4326)

4. Read and process the NYS retail food stores data

food_stores_df <- read.csv("./R-Spatial_I_Lab/NYS_Retail_Food_Stores.csv")
food_stores_df %>%
  tidyr::separate(Location, c(NA, "Both", NA), sep = "(\\(|\\))") %>%
  tidyr::separate(Both, c("Lat", "Long"), sep = ",") %>%
  dplyr::mutate(Lat = as.numeric(Lat), Long = as.numeric(Long)) %>%
  tidyr::drop_na(Lat, Long) %>%
  sf::st_as_sf(coords = c('Long', 'Lat')) %>%
  st_set_crs(4326) -> food_stores_sf

5. Use simple mapping method

mapview(nyc_zip)
mapview(health_services_sf)
mapview(food_stores_sf)

6. Save the three sf objects

st_write(nyc_zip, dsn = "./R-Spatial_I_Lab/nyc_zip.gpkg",
         layer = 'nyc_zip_code',
         delete_layer = TRUE)
## Deleting layer `nyc_zip_code' using driver `GPKG'
## Writing layer `nyc_zip_code' to data source 
##   `./R-Spatial_I_Lab/nyc_zip.gpkg' using driver `GPKG'
## Writing 263 features with 12 fields and geometry type Polygon.
st_write(health_services_sf, dsn = "./R-Spatial_I_Lab/health_services.gpkg",
         layer = 'NY_Health_Services', delete_layer = TRUE)
## Deleting layer `NY_Health_Services' using driver `GPKG'
## Writing layer `NY_Health_Services' to data source 
##   `./R-Spatial_I_Lab/health_services.gpkg' using driver `GPKG'
## Writing 3844 features with 34 fields and geometry type Point.
st_write(food_stores_sf, dsn = './R-Spatial_I_Lab/food_stores.gpkg',
         layer = 'Food_Stores', delete_layer = TRUE)
## Deleting layer `Food_Stores' using driver `GPKG'
## Writing layer `Food_Stores' to data source 
##   `./R-Spatial_I_Lab/food_stores.gpkg' using driver `GPKG'
## Writing 23972 features with 14 fields and geometry type Point.