R Spatial Lab Assignment 2

Task 0: Set Up New Project and Load Week 7 Saved Data

library(tidyverse)

library(sf)

library(mapview)

library(janitor)

wd <- getwd() #Get working directory

data_dir <- file.path(wd,“data1”) #Set up file path

list.files(“data1”) #See what data files in the data1 folder

nyc_zip <- st_read(“data1/nyc_data.gpkg”, layer=“zip”) #Load each of the data files saved

food_retial <- st_read(“data1/nyc_data.gpkg”, layer=“food”)

health_fac <- st_read(“data1/nyc_data.gpkg”, layer=“health”)

unzip(file.path(data_dir,“2010 Census Tracts.zip”), exdir=data_dir) #Up zip files from 2010 Census Tracts.zip tracts_2010 <- st_read(file.path(data_dir, “geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp”)) #Read 2010 tract data for later

Task 1: Join COVID-19 Data to NYC Zip Codes

covid <- read_csv(file.path(data_dir, “tests-by-zcta_2020_04_19.csv”)) #Read csv file for COVID-19 data

colnames(covid) #Identifying the names of the columns

#Look at the data types for ZIPCODE and MODZCTA str(nyc_zip\(ZIPCODE) str(covid\)MODZCTA)

covid <- covid %>% mutate(MODZCTA = as.character(MODZCTA)) #Converts MODZCTA from number to character

zip_covid <- nyc_zip %>% left_join(covid, by = c(“ZIPCODE” = “MODZCTA”)) #Run spatial join

Task 2: Aggregate NYC Food Retails data to NYC Zip code data

colnames(food_retial) #Identifying the names of the columns

table(food_retial$establishment_type) #Identify the different combinations using the data dictionary provided

zip_food <- food_retial %>% mutate(establishment_type = trimws(establishment_type)) %>% #Clean out empty spaces within establishment_types filter(str_detect(establishment_type, “^(A|B|J)”)) %>% #Filtering out establishment types A,B and J st_transform(st_crs(zip_covid)) %>% #Transform to sf st_join(zip_covid, ., join = st_contains) %>% #Spatial Join group_by(ZIPCODE) %>% #Aggregation summarise( food_store_count = n(), Positive = first(Positive), Total = first(Total) )

Task 3: Aggregate NYC Health Facilities data to NYC Zip code data

colnames(health_fac) #Identifying the names of the columns

zip_health <- health_fac %>% filter(description == “Diagnostic and Treatment Center”) %>% #Filter out Diagnostic and Treatment Center data st_transform(st_crs(nyc_zip)) %>% #Transform to sf st_join(zip_food, ., join = st_contains) %>% #Spatial Join group_by(ZIPCODE) %>% #Aggregation summarise( food_store_count = first(food_store_count), health_count = n(), Positive = first(Positive), Total = first(Total) )

Task 4: Join Census ACS data to NYC Planning Census Tract data

acs <- read_csv( file.path(data_dir, “ACSDP5Y2018.DP05_data_with_overlays_2020-04-22T132935.csv”), skip = 1 ) # Read ACS data

colnames(acs) #Identifying the names of the columns

acs <- acs %>% select( id, totPop = Estimate!!SEX AND AGE!!Total population ) %>% mutate(tract_full = str_sub(id, -11, -1)) #Simplify name and creates new column for join

tracts_2010 <- tracts_2010 %>% mutate( cntyFIPS = case_when( boro_name == “Bronx” ~ “005”, boro_name == “Brooklyn” ~ “047”, boro_name == “Manhattan” ~ “061”, boro_name == “Queens” ~ “081”, boro_name == “Staten Island” ~ “085” ), tract_full = paste0(“36”, cntyFIPS, sprintf(“%06d”, as.numeric(ct2010))) )#Fix tracts for matching to ACS data

popData <- merge(tracts_2010, acs, by = “tract_full”) #Join to ACS

Task 5: Aggregate the ACS Census data to zip code area data

popNYC <- st_transform(popData, st_crs(zip_health)) #Join ACS data to zip codes

zip_final <- st_join( zip_health, popNYC %>% st_centroid(), join = st_contains ) %>% group_by(ZIPCODE, Positive, Total, food_store_count, health_count) %>% summarise( total_population = sum(totPop, na.rm = TRUE) ) #Spatial Join and Aggregation all together

head(zip_final) #Final check that everything is together