Explanation of the template

Update the title with your information. Make sure to include identification information so that we know it is your submission.

Also update the author name and date accordingly.

Check out the Source Code from the top-right corner </>Code menu.

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 # 2

Don’t use a single chunk for the entire assignment. Break it into multiple chunks.

task 1:

nyc_covid_data_testing<-read_csv("C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/Section_08/R-Spatial_II_Lab/R-Spatial_II_Lab/tests-by-zcta_2020_04_12.csv")
## 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.
nyc_postal <- st_read("C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `C:\Users\dwvil\Documents\SPRING 2025\R LANGUAGE\R-Spatial\R-Spatial\Data\R-Spatial_I_Lab\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)
NYS_Health_Facility<-read_csv("C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/R-Spatial_I_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.
NYS_Retail_Food_Stores <- read_csv(
  "C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/R-Spatial_I_Lab/nys_retail_food_Store_xy.csv", 
  locale = locale(encoding = "Latin1")
)
## Rows: 29389 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): ï..County, Operation.Type, Establishment.Type, Entity.Name, DBA.Na...
## dbl  (4): License.Number, Zip.Code, Y, X
## num  (1): Square.Footage
## lgl  (2): Address.Line.2, Address.Line.3
## 
## ℹ 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.
nyc_covid_data_testing <- nyc_covid_data_testing %>%
  mutate(MODZCTA = as.character(MODZCTA))

nyc_postal_Joined <- nyc_postal %>%
  left_join(nyc_covid_data_testing, by = c("ZIPCODE" = "MODZCTA"))

task 2:

Quarto markdown is different from R markdown in terms of chunk options. See chunk options at Quarto website.

nycFoodStoreSF <- NYS_Retail_Food_Stores %>%
  dplyr::filter(Establishment.Type %in% c("A")) %>%  # Filtering specific food store types
  dplyr::group_by(Zip.Code) %>%
  dplyr::summarise(FoodStoreNum = n())

# Convert Zip.Code to character to match ZIPCODE
nycFoodStoreSF <- nycFoodStoreSF %>%
  dplyr::mutate(Zip.Code = as.character(Zip.Code))

# Perform the join
nyc_postal_food_storesA <- nyc_postal %>%
  left_join(nycFoodStoreSF, by = c("ZIPCODE" = "Zip.Code"))

task 3:

# Filter for nursing homes (NH) from the health facility dataset
nycNursingHome <- NYS_Health_Facility %>%
  dplyr::filter(`Short Description` == "NH") %>%
  dplyr::group_by(`Facility Zip Code`)

nyc_postal_nursing_homes <- nyc_postal %>%
  left_join(nycNursingHome, by = c("ZIPCODE" = "Facility Zip Code"))
## Warning in sf_column %in% names(g): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 6 of `x` matches multiple rows in `y`.
## ℹ Row 6 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.

task 4:

nycCensus <- sf::st_read('C:/Users/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/Section_08/R-Spatial_II_Lab/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\dwvil\Documents\SPRING 2025\R LANGUAGE\R-Spatial\R-Spatial\Data\Section_08\R-Spatial_II_Lab\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)
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/dwvil/Documents/SPRING 2025/R LANGUAGE/R-Spatial/R-Spatial/Data/Section_08/R-Spatial_II_Lab/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, # >= 65
                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));

acsData %>%
  magrittr::extract(1:10,)
##                  GEO_ID totPop elderlyPop malePop femalePop whitePop blackPop
## 1  1400000US36005000100   7080         51    6503       577     1773     4239
## 2  1400000US36005000200   4542        950    2264      2278     2165     1279
## 3  1400000US36005000400   5634        710    2807      2827     2623     1699
## 4  1400000US36005001600   5917        989    2365      3552     2406     2434
## 5  1400000US36005001900   2765         76    1363      1402      585     1041
## 6  1400000US36005002000   9409        977    4119      5290     3185     4487
## 7  1400000US36005002300   4600        648    2175      2425      479     2122
## 8  1400000US36005002400    172          0     121        51       69       89
## 9  1400000US36005002500   5887        548    2958      2929      903     1344
## 10 1400000US36005002701   2868        243    1259      1609      243      987
##    asianPop hispanicPop adultPop citizenAdult censusCode
## 1       130        2329     6909         6100  005000100
## 2       119        3367     3582         2952  005000200
## 3       226        3873     4507         4214  005000400
## 4        68        3603     4416         3851  005001600
## 5       130        1413     2008         1787  005001900
## 6        29        5905     6851         6170  005002000
## 7        27        2674     3498         3056  005002300
## 8        14           0      131           42  005002400
## 9        68        4562     4237         2722  005002500
## 10        0        1985     1848         1412  005002701
popData <- merge(nycCensus, acsData, by.x ='tractFIPS', by.y = 'censusCode')

task 5:

nycCensus <- st_transform(nycCensus, st_crs(nyc_postal))

popData <- nycCensus %>%
  left_join(acsData, by = c("tractFIPS" = "censusCode"))

nycCensus_ZIP <- st_join(popData, nyc_postal, join = st_intersects)
colnames(nycCensus_ZIP)
##  [1] "boro_code"    "boro_ct201"   "boro_name"    "cdeligibil"   "ct2010"      
##  [6] "ctlabel"      "ntacode"      "ntaname"      "puma"         "shape_area"  
## [11] "shape_leng"   "cntyFIPS"     "tractFIPS"    "GEO_ID"       "totPop"      
## [16] "elderlyPop"   "malePop"      "femalePop"    "whitePop"     "blackPop"    
## [21] "asianPop"     "hispanicPop"  "adultPop"     "citizenAdult" "ZIPCODE"     
## [26] "BLDGZIP"      "PO_NAME"      "POPULATION"   "AREA"         "STATE"       
## [31] "COUNTY"       "ST_FIPS"      "CTY_FIPS"     "URL"          "SHAPE_AREA"  
## [36] "SHAPE_LEN"    "geometry"
acs_zip_aggregated <- nycCensus_ZIP %>%
  dplyr::group_by(ZIPCODE) %>%
  dplyr::summarise(across(c(totPop, elderlyPop, malePop, femalePop, 
                            whitePop, blackPop, asianPop, hispanicPop, 
                            adultPop, citizenAdult), sum, na.rm = TRUE))
## Warning: There was 1 warning in `dplyr::summarise()`.
## ℹ In argument: `across(...)`.
## ℹ In group 1: `ZIPCODE = "00083"`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
## 
##   # Previously
##   across(a:b, mean, na.rm = TRUE)
## 
##   # Now
##   across(a:b, \(x) mean(x, na.rm = TRUE))
nyc_postal <- st_join(nyc_postal, acs_zip_aggregated, join = st_intersects)

nyc_postal <- st_as_sf(nyc_postal)