task 1: Join the COVID-19 data to the NYC zip code area data (sf or
sp polygons).
# load in RData file from assignment #1 with processed datasets as sf objects
load("R-Spatial_II_Lab/assignment1_data.RData")
# read in covid test data
covid_tests_04_12_2020 <- readr::read_csv("R-Spatial_II_Lab/tests-by-zcta_2020_04_12.csv", lazy = FALSE, show_col_types = FALSE)
colnames(covid_tests_04_12_2020) <- c("zipcode", "posTests_04_12_2020", "totTests__04_12_2020", "pcPosTests_04_12_2020")
# join covid data with zip code sf
sf_nyc_zip_merged <- base::merge(nyc_zip_sf, covid_tests_04_12_2020, by.x = "ZIPCODE", by.y = "zipcode")
# map merged sf to view and confirm
mapview::mapview(sf_nyc_zip_merged, zcol = 'posTests_04_12_2020', layer.name = "Positive COVID Tests 04-12-20 by Zip Code")
task 2: Aggregate the NYC food retails store data (points) to the
zip code data, so that we know how many retail stores in each zip code
area. Note that not all locations are for food retail. And we need to
choose the specific types according to the data.
# filter food store type and count the number contained per zip code
nyc_tot_foodStores_zc <- nyc_rfs %>% dplyr::filter(stringr::str_detect(establishment_type, '[AJD]')) %>%
sf::st_join(sf_nyc_zip_merged, ., join = st_contains) %>%
group_by(ZIPCODE) %>%
summarise(tot_foodStores = n()) %>%
st_drop_geometry()
# join total number of food stores by zip code to zip code sf
sf_nyc_zip_merged <- merge(sf_nyc_zip_merged, nyc_tot_foodStores_zc, by.x = "ZIPCODE", by.y = "ZIPCODE")
# map total food stores by zip code
mapview::mapview(sf_nyc_zip_merged, zcol = 'tot_foodStores', layer.name = "Total Food Stores by Zip Code")