This Rpub includes the code used for week 08’ lab.
In the following R code chunk, load_packages is the code chunk name. include=FALSE suggests that the code chunk will run, but the code itself and its outputs will not be included in the rendered HTML. echo=TRUE in the following code chunk suggests that the code and results from running the code will be included in the rendered HTML.
R Spatial Lab Assignment # 8
Don’t use a single chunk for the entire assignment. Break it into multiple chunks.
task 1:
Join the COVID-19 data to the NYC zip code area data (sf or sp polygons).
Rows: 178 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (4): MODZCTA, Positive, Total, zcta_cum.perc_pos
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Step 2, Reading in the shapefile to a sf. nyctracts2010 <-st_read("D:/R-Spatial/Data/R-Spatial_II_Lab/geo_export_213dae92-dbda-4a0a-bf58-bfee89c37e04.shp")
Reading layer `geo_export_213dae92-dbda-4a0a-bf58-bfee89c37e04' from data source `D:\R-Spatial\Data\R-Spatial_II_Lab\geo_export_213dae92-dbda-4a0a-bf58-bfee89c37e04.shp'
using driver `ESRI Shapefile'
Simple feature collection with 178 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
Geodetic CRS: WGS 84
Show the code
#Merging the census data into the census tract.nyc412 <- nyc412 %>%rename(zcta = MODZCTA)nyc_covid <- base::merge(nyctracts2010, nyc412, by.x ="zcta", by.y ="zcta")names(nyc_covid)
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.
Show the code
#Aggregating the DataNYS_Food_Retail_Stores <-st_read("D:/R-Spatial/Data/R-Spatial_II_Lab/nycFoodStore.shp")
Reading layer `nycFoodStore' from data source
`D:\R-Spatial\Data\R-Spatial_II_Lab\nycFoodStore.shp' using driver `ESRI Shapefile'
Simple feature collection with 11300 features and 16 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -74.2484 ymin: 40.50782 xmax: -73.67061 ymax: 40.91008
Geodetic CRS: WGS 84
Simple feature collection with 6 features and 24 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -73.9975 ymin: 40.70912 xmax: -73.97351 ymax: 40.72414
Geodetic CRS: WGS 84
zcta modzcta label pop_est Positive Total zcta_cum.perc_pos tract_area
1 10002 10002 10002 74993 539 1024 52.64 2278152 [m^2]
1.1 10002 10002 10002 74993 539 1024 52.64 2278152 [m^2]
1.2 10002 10002 10002 74993 539 1024 52.64 2278152 [m^2]
1.3 10002 10002 10002 74993 539 1024 52.64 2278152 [m^2]
1.4 10002 10002 10002 74993 539 1024 52.64 2278152 [m^2]
1.5 10002 10002 10002 74993 539 1024 52.64 2278152 [m^2]
ï__Cnty Lcns_Nm Oprtn_T Estbl_T Entty_N
1 New York 628095 Store JAC FERNANDEZ JORGE
1.1 New York 629082 Store JAC 124 DELI GROCERY INC
1.2 New York 628413 Store JAC 208 RIVINGTON INC
1.3 New York 620979 Store JACK 47 DIVISION STREET TRADING INC
1.4 New York 627543 Store JAC 47 PITT GROCERY INC
1.5 New York 701940 Store JAC 51 EAST BROADWAY MARKET INC
DBA_Nam Strt_Nmb Stret_Nm Add_L_2 Add_L_3 City
1 122 SUFFOLK STOP 1 DELI 122 SUFFOLK ST NA NA NEW YORK
1.1 124 DELI GROCERY 124 E BROADWAY NA NA NEW YORK
1.2 208 RIVINGTON 208 RIVINGTON ST NA NA NEW YORK
1.3 47 DIVISION ST TRADING 47 DIVISION ST NA NA NEW YORK
1.4 47 PITT GROCERY 47 PITT ST NA NA NEW YORK
1.5 51 EAST BROADWAY MARKET 51 E BROADWAY NA NA NEW YORK
State Zip_Cod Sqr_Ftg
1 NY 10002 1,000
1.1 NY 10002 1,500
1.2 NY 10002 0
1.3 NY 10002 2,000
1.4 NY 10002 1,500
1.5 NY 10002 2,500
Locatin
1 122 SUFFOLK ST\nNEW YORK, NY 10002\n(40.719204, -73.985959)
1.1 124 E BROADWAY\nNEW YORK, NY 10002\n(40.713896, -73.992096)
1.2 208 RIVINGTON ST\nNEW YORK, NY 10002\n(40.718419, -73.98291)
1.3 47 DIVISION ST\nNEW YORK, NY 10002\n(40.714205, -73.995252)
1.4 47 PITT ST\nNEW YORK, NY 10002\n(40.717311, -73.982991)
1.5 51 E BROADWAY\nNEW YORK, NY 10002\n(40.7136, -73.995653)
Coords geometry
1 40.719204, -73.985959 MULTIPOLYGON (((-73.9975 40...
1.1 40.713896, -73.992096 MULTIPOLYGON (((-73.9975 40...
1.2 40.718419, -73.98291 MULTIPOLYGON (((-73.9975 40...
1.3 40.714205, -73.995252 MULTIPOLYGON (((-73.9975 40...
1.4 40.717311, -73.982991 MULTIPOLYGON (((-73.9975 40...
1.5 40.7136, -73.995653 MULTIPOLYGON (((-73.9975 40...
Aggregate the NYC health facilities (points) to the zip code data. Similarly, choose appropriate subtypes such as nursing homes from the facilities.
Show the code
#Aggregating the health dataNYS_health<-read_csv("D:/R-Spatial/Data/R-Spatial_II_Lab/NYS_Health_Facility.csv")
Rows: 3990 Columns: 36
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (28): Facility Name, Short Description, Description, Facility Open Date,...
dbl (8): Facility ID, Facility Phone Number, Facility Fax Number, Facility ...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Show the code
NYS_HealthAg <- NYS_health %>% dplyr::filter(`Short Description`=="NH") %>% dplyr::group_by(`Facility Zip Code`)nyc_healthjoin <- nyctracts2010 %>%left_join(NYS_HealthAg, by =c("zcta"="Facility Zip Code"))
task 4:
Join the Census ACS population, race, and age data to the NYC Planning Census Tract Data.
Show the code
#Join the Census ACS population, race, and age data to the NYC Planning Census Tract Data.nycCensus <- sf::st_read("C:/Users/campb/Downloads/R-Spatial_II_Lab/2010 Census Tracts/geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp", stringsAsFactors =FALSE)
Reading layer `geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a' from data source `C:\Users\campb\Downloads\R-Spatial_II_Lab\2010 Census Tracts\geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp'
using driver `ESRI Shapefile'
Simple feature collection with 2165 features and 11 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
Geodetic CRS: WGS84(DD)
Show the code
str(nycCensus)
Classes 'sf' and 'data.frame': 2165 obs. of 12 variables:
$ boro_code : chr "5" "1" "1" "1" ...
$ boro_ct201: chr "5000900" "1009800" "1010000" "1010200" ...
$ boro_name : chr "Staten Island" "Manhattan" "Manhattan" "Manhattan" ...
$ cdeligibil: chr "E" "I" "I" "I" ...
$ ct2010 : chr "000900" "009800" "010000" "010200" ...
$ ctlabel : chr "9" "98" "100" "102" ...
$ ntacode : chr "SI22" "MN19" "MN19" "MN17" ...
$ ntaname : chr "West New Brighton-New Brighton-St. George" "Turtle Bay-East Midtown" "Turtle Bay-East Midtown" "Midtown-Midtown South" ...
$ puma : chr "3903" "3808" "3808" "3807" ...
$ shape_area: num 2497010 1906016 1860938 1860993 1864600 ...
$ shape_leng: num 7729 5534 5692 5688 5693 ...
$ geometry :sfc_MULTIPOLYGON of length 2165; first list element: List of 1
..$ :List of 1
.. ..$ : num [1:28, 1:2] -74.1 -74.1 -74.1 -74.1 -74.1 ...
..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
..- attr(*, "names")= chr [1:11] "boro_code" "boro_ct201" "boro_name" "cdeligibil" ...
---title: "R Week 08 Assignment"author: "Victoria Campbell"date: "2/22/2025"format: html: toc: true toc-location: left code-fold: true code-summary: "Show the code" code-tools: true---## Explanation of the SiteThis Rpub includes the code used for week 08' lab.```{r setup, include=FALSE}knitr::opts_chunk$set(echo = TRUE)```In the following R code chunk, `load_packages` is the code chunk name. `include=FALSE` suggests that the code chunk will run, but the code itself and its outputs will not be included in the rendered HTML. `echo=TRUE` in the following code chunk suggests that the code and results from running the code will be included in the rendered HTML.```{r load_packages, include=FALSE}require(tidyverse);require(sf); require(mapview); require(magrittr)require(sp)require(raster)require(dplyr)require(readr)require(stringr)```## R Spatial Lab Assignment \# 8Don't use a single chunk for the entire assignment. Break it into multiple chunks.### task 1:Join the COVID-19 data to the NYC zip code area data (sf or sp polygons).```{r R-spatial-assignment-task 1, echo=TRUE}##04, 12, 2020 datanyc412<- readr::read_csv("C:/Users/campb/Downloads/R-Spatial_II_Lab/tests-by-zcta_2020_04_12.csv", lazy = FALSE)str(nyc412)#Step 2, Reading in the shapefile to a sf. nyctracts2010 <- st_read("D:/R-Spatial/Data/R-Spatial_II_Lab/geo_export_213dae92-dbda-4a0a-bf58-bfee89c37e04.shp")#Merging the census data into the census tract.nyc412 <- nyc412 %>% rename(zcta = MODZCTA)nyc_covid <- base::merge(nyctracts2010, nyc412, by.x = "zcta", by.y = "zcta")names(nyc_covid) ```### 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.```{r}#| label: R-spatial-assignment-task 2#| echo: TRUE#Aggregating the DataNYS_Food_Retail_Stores <-st_read("D:/R-Spatial/Data/R-Spatial_II_Lab/nycFoodStore.shp")nyc_covid %>%mutate(tract_area =st_area(geometry)) %>%head()nyc_covid %>%mutate(tract_area =st_area(geometry)) %>%st_transform(4326) %>%st_join(NYS_Food_Retail_Stores) %>%head()nyc_food_sf <- nyctracts2010 %>%mutate(tract_area =st_area(geometry)) %>%st_transform(4326) %>%st_join(NYS_Food_Retail_Stores) %>%group_by(zcta)```### task 3:Aggregate the NYC health facilities (points) to the zip code data. Similarly, choose appropriate subtypes such as nursing homes from the facilities.```{r}#| label: R-spatial-assignment-task 3#| echo: TRUE#Aggregating the health dataNYS_health<-read_csv("D:/R-Spatial/Data/R-Spatial_II_Lab/NYS_Health_Facility.csv")NYS_HealthAg <- NYS_health %>% dplyr::filter(`Short Description`=="NH") %>% dplyr::group_by(`Facility Zip Code`)nyc_healthjoin <- nyctracts2010 %>%left_join(NYS_HealthAg, by =c("zcta"="Facility Zip Code"))```### task 4:Join the Census ACS population, race, and age data to the NYC Planning Census Tract Data.```{r}#| label: R-spatial-assignment-task 4#| echo: TRUE#Join the Census ACS population, race, and age data to the NYC Planning Census Tract Data.nycCensus <- sf::st_read("C:/Users/campb/Downloads/R-Spatial_II_Lab/2010 Census Tracts/geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp", stringsAsFactors =FALSE)str(nycCensus)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=''))acsData <-readLines("C:/Users/campb/Downloads/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, # >= 65malePop = 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));acsData %>% magrittr::extract(1:10,)# Merge (JOIN) ACS data to the census tractspopData <-merge(nycCensus, acsData, by.x ='ct2010', by.y ='censusCode')```### task 5:Aggregate the ACS census data to zip code area data.```{r}#| label: R-spatial-assignment-task 5#| echo: TRUEpopData <-st_transform(popData, st_crs(nyc_covid))covidPopZipNYC <-st_join( nyc_covid,st_centroid(popData),join = st_contains) %>%group_by(modzcta, label, pop_est, Positive, Total, zcta_cum.perc_pos) %>%summarise(totPop =sum(totPop, na.rm =TRUE),malePctg =sum(malePop, na.rm =TRUE) / totPop *100,asianPop =sum(asianPop, na.rm =TRUE),blackPop =sum(blackPop, na.rm =TRUE),hispanicPop =sum(hispanicPop, na.rm =TRUE),whitePop =sum(whitePop, na.rm =TRUE),.groups ="drop" )```