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.

Import COVID data

covid_2021_04_23 <- st_read(‘R-Spatial_II_Lab/tests-by-zcta_2021_04_23.csv’, stringsAsFactors = FALSE)

Join the COVID-19 data to the NYC zip code area data (sf or sp polygons).

covid_merge <- base::merge(covid_2021_04_23, nyc_040114, by.x = “MODIFIED_ZCTA”, by.y = “ZIPCODE”) # Make sf covid_merge_sf <- st_as_sf(covid_merge)

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.

Reprojection

nys_rfs_food <- st_transform(nys_rfs_sf, st_crs(nyc_040114))

FIlter business type and counties

nyc_food <- nys_rfs_food %>% filter( Establishment.Type == “A”, County %in% c(“New York”, “Kings”, “Queens”, “Bronx”, “Richmond”) )

Joining food data and zipcode data

nyc_food_merge <- st_join(nyc_food, nyc_040114, join = st_within)

Filter out rows with NA values, group data by zipcode and get no. of food

stores in each zipcode

zip_food_count <- nyc_food_merge %>% filter(!is.na(ZIPCODE)) %>% st_drop_geometry() %>% group_by(ZIPCODE) %>% summarise(store_count = n())

Re join with zipcode geometry

nyc_zip_counts <- nyc_040114 %>% left_join(zip_food_count, by = “ZIPCODE”)

mapview(nyc_zip_counts , zcol = “store_count”)

Aggregate the NYC health facilities (points) to the zip code data. Similarly,

choose appropriate subtypes such as nursing homes from the facilities.

Reprojection

nys_hf_sf <- st_transform(nys_hf_sf, st_crs(nyc_040114))

FIlter NH and counties

nyc_nh <- nys_hf_sf %>% filter( Short.Description == “NH”, Facility.County %in% c(“New York”, “Kings”, “Queens”, “Bronx”, “Richmond”) )

Joining nh and zipcode data

nyc_nh_merge <- st_join(nyc_nh, nyc_040114, join = st_within)

Filter out rows with NA values, group data by zipcode and get no. of food

stores in each zipcode

nyc_nh_count <- nyc_nh_merge %>% filter(!is.na(ZIPCODE)) %>% st_drop_geometry() %>% group_by(ZIPCODE) %>% summarise(nh_count = n())

Re join with zipcode geometry

nyc_nh_count <- nyc_040114 %>% left_join(nyc_nh_count, by = “ZIPCODE”)

mapview

mapview(nyc_nh_count , zcol = “nh_count”)

Join the Census ACS population, race, and age data to the NYC Planning Census

Tract Data

Import census tract data

nycCensus %<>% dplyr::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’), tractFIPS = paste(cntyFIPS, ct2010, sep=’’) )

Import census tract data

ACS Data

I had a bit of trouble understanding all of the lines of this bit of code.

acsData <- readLines( “R-Spatial_II_Lab/ACSDP5Y2018.DP05_data_with_overlays_2020-04-22T132935.csv” ) %>% magrittr::extract(-2) %>% textConnection() %>% read.csv(header=TRUE, quote= “"”) %>% dplyr::select(GEO_ID, totPop = DP05_0001E, elderlyPop = DP05_0024E, malePop = DP05_0002E, femalePop = DP05_0003E,
whitePop = DP05_0037E, blackPop = DP05_0038E, asianPop = DP05_0067E, hispanicPop = DP05_0071E, adultPop = DP05_0021E, citizenAdult = DP05_0087E) %>% dplyr::mutate(censusCode = stringr::str_sub(GEO_ID, -9,-1));

merge census and ACS data

popData <- merge(nycCensus, acsData, by.x =‘tractFIPS’, by.y = ‘censusCode’)

align coordinate system with zip code data

popData <- sf::st_transform(popData, st_crs(nyc_040114))

Aggregate the ACS census data to zip code area data.

joining COVID zip code data with the census/acs combo data

converting census tracts (polygons) to points

Performing group by and summarizing the population data to organize the

data

covidPopZipNYC <- sf::st_join(covid_merge_sf, popData %>% sf::st_centroid(), join = st_contains) %>% group_by(MODIFIED_ZCTA, PO_NAME, POPULATION, COUNTY, COVID_CASE_COUNT, TOTAL_COVID_TESTS) %>% summarise(totPop = sum(totPop), malePctg = sum(malePop)/totPop*100, asianPop = sum(asianPop), blackPop = sum(blackPop), hispanicPop = sum(hispanicPop), whitePop = sum(whitePop))