For this lab I read the provided NYC zip code shapefile into a sf object. I also read the provided csvs first into df objects and then sfs with appropriate cleaning applied. Due to the presence of NA values and anomalous coordinates, some records were excluded from the final display.
library("sf")
library("tidyverse")
library("janitor")
library("ggplot2")
library("ggmap")
library("mapview")# I set the file path based on how Prof. Marcotullio described in the previous section
wd <- getwd()
zip_path <- file.path(wd, "Data", "ZIP_CODE_040114", "ZIP_CODE_040114.shp")
zip_codes_sf <- st_read(zip_path)## Reading layer `ZIP_CODE_040114' from data source
## `C:\Users\Sam\Documents\School\Hunter\GEOG393\Section_3\Lecture_7\Data\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)
# head(zip_codes_sf)
health_facility_path <- file.path(wd, "Data", "NYS_Health_Facility.csv")
retail_food_stores_path <- file.path(wd, "Data", "NYS_Retail_Food_Stores.csv")
retail_food_store_xy_path <- file.path(wd, "Data", "nys_retail_food_store_xy.csv")
# I clean the names and mutate a col into numerics for the join later
health_facility_df <- read_csv(health_facility_path, show_col_types = FALSE, lazy = FALSE) %>%
clean_names()
retail_food_stores_df <- read_csv(retail_food_stores_path, show_col_types = FALSE, lazy = FALSE) %>%
clean_names() %>%
mutate(license_number = as.numeric(license_number))
retail_food_store_xy_df <- read_csv(retail_food_store_xy_path, show_col_types = FALSE, lazy = FALSE) %>%
clean_names()
# str(health_facility_df)
# head(retail_food_stores_df)
# head(retail_food_store_xy_df)
# Getting the sf of the Health Facilities, cleaning any NAs as they will be useless
# I also noted one record with a value of 0.0 for its coords, this looked anomalous so I removed
# Another record had a location that put it in Antarctica so I filtered it as well
# Added code to filter out all facilities not in NYC by county
health_facility_sf <- health_facility_df %>%
filter(!is.na(facility_longitude) & !is.na(facility_latitude)) %>%
filter(facility_county %in% c("Bronx", "Kings", "New York", "Queens", "Richmond")) %>%
filter(!((facility_longitude == 0) & (facility_latitude == 0))) %>%
filter(facility_id != 5764) %>%
st_as_sf(coords = c("facility_longitude", "facility_latitude"), crs = 4326)
# str(health_facility_sf)
# st_crs(zip_codes_sf)
# st_crs(health_facility_sf)
# Retail stores needs to be joined first because there are no coords on the main file
retail_food_stores_combined_df <- retail_food_stores_df %>%
left_join(retail_food_store_xy_df, by = "license_number")
# head(retail_food_stores_combined_df)
# Have to clean some NAs
retail_food_stores_combined_df <- retail_food_stores_combined_df %>%
select_if(~ !all(is.na(.)))
# now we can make the df a sf & remove non NYC stores
retail_food_stores_sf <- retail_food_stores_combined_df %>%
filter(county.x %in% c("Bronx", "Kings", "New York", "Queens", "Richmond")) %>%
filter(!is.na(x) & !is.na(y)) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
# str(retail_food_stores_sf)
# st_crs(retail_food_stores_sf)This is where I mapped the csvs using the mapview package.I transformed the zip codes to WGS1984 for continuity with the other sfs.