R Week 08 Assignment

Author

Caitlin Cacciatore

Published

March 20, 2026

install.packages(‘knitr’)

library(knitr)

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.

Show the code
require(tidyverse);
Loading required package: tidyverse
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Show the code
require(sf); 
Loading required package: sf
Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
Show the code
require(mapview); 
Loading required package: mapview
Show the code
require(magrittr)
Loading required package: magrittr

Attaching package: 'magrittr'

The following object is masked from 'package:purrr':

    set_names

The following object is masked from 'package:tidyr':

    extract

R Spatial Lab Assignment # 1

#Load the Packages

Show the code
#Loading the Packages

options(repos = c(CRAN = "https://cloud.r-project.org"))

# Load a list of packages. Install them first if they are not available.
# The list of packages to be installed
list.of.packages <- c("sf", "sp", "spatial", "maptools", "rgeos","rgdal",
                      "raster", "grid", "rasterVis",
                      "tidyverse", "magrittr", "ggpubr", "lubridate",
                      "devtools", "htmlwidgets", "mapview",
                      "classInt", "RColorBrewer", "ggmap", "tmap", "leaflet", "mapview",
                      "ggrepel", "ggsn",
                      "spdep","spatialreg","GWmodel");

# Check out the packages that have not been installed yet.
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]

# Install those missing packages first. It could take a long time for the first time.
if(length(new.packages)>0) install.packages(new.packages)
Warning: packages 'maptools', 'rgeos', 'rgdal', 'ggsn' are not available for this version of R

Versions of these packages for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
Show the code
# Load all packages.

lapply(list.of.packages,function(x) {
  require(x,character.only = TRUE,quietly = TRUE)
})

Attaching package: 'raster'
The following object is masked from 'package:dplyr':

    select

Attaching package: 'ggpubr'
The following object is masked from 'package:raster':

    rotate
ℹ Google's Terms of Service: <https://mapsplatform.google.com>
  Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service>
  OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles>
ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.

Attaching package: 'ggmap'


The following object is masked from 'package:magrittr':

    inset


To access larger datasets in this package, install the spDataLarge
package with: `install.packages('spDataLarge',
repos='https://nowosad.github.io/drat/', type='source')`


Attaching package: 'Matrix'


The following objects are masked from 'package:tidyr':

    expand, pack, unpack



Attaching package: 'spatialreg'


The following objects are masked from 'package:spdep':

    get.ClusterOption, get.coresOption, get.mcOption,
    get.VerboseOption, get.ZeroPolicyOption, set.ClusterOption,
    set.coresOption, set.mcOption, set.VerboseOption,
    set.ZeroPolicyOption
[[1]]
[1] TRUE

[[2]]
[1] TRUE

[[3]]
[1] TRUE

[[4]]
[1] FALSE

[[5]]
[1] FALSE

[[6]]
[1] FALSE

[[7]]
[1] TRUE

[[8]]
[1] TRUE

[[9]]
[1] TRUE

[[10]]
[1] TRUE

[[11]]
[1] TRUE

[[12]]
[1] TRUE

[[13]]
[1] TRUE

[[14]]
[1] TRUE

[[15]]
[1] TRUE

[[16]]
[1] TRUE

[[17]]
[1] TRUE

[[18]]
[1] TRUE

[[19]]
[1] TRUE

[[20]]
[1] TRUE

[[21]]
[1] TRUE

[[22]]
[1] TRUE

[[23]]
[1] TRUE

[[24]]
[1] FALSE

[[25]]
[1] TRUE

[[26]]
[1] TRUE

[[27]]
[1] FALSE
Show the code
install.packages("sf")     # run once if not installed

The downloaded binary packages are in
    /var/folders/dm/sj7012g577qdnv76gj2_szd80000gp/T//RtmpnFiOu8/downloaded_packages
Show the code
install.packages("tidyverse")

The downloaded binary packages are in
    /var/folders/dm/sj7012g577qdnv76gj2_szd80000gp/T//RtmpnFiOu8/downloaded_packages
Show the code
library(sf)
library(tidyverse)

Health Facilities Code

Assignment begins here

Task 1

Read the COVID data for one week

covid_data <- readr::read_csv(“tests-by-zcta_2021_04_23.csv”, lazy = FALSE) str(covid_data)

Merge Zip Code and COVID Data

nyc_covid_data_sf_merged <- base::merge(nyc_zip_sf, covid_data, by.x = “ZIPCODE”, by.y = “MODIFIED_ZCTA”) names(nyc_covid_data_sf_merged)

Task 2

#Read NYS Retail Food Store Data

nys_retail <- readr::read_csv(“NYS_Retail_Food_Stores.csv”, lazy = FALSE) str(nys_retail)

Merge food stores and Zip Code Data

nyc_food_stores <- base::merge(nyc_zip_sf, nys_retail, by.x = “ZIPCODE”, by.y = “Zip Code”) names(nyc_food_stores)

Aggregate by Zip Code

zip_summary_sf <- nyc_food_stores %>% group_by(ZIPCODE) %>% summarise(store_count = n())

check the names of the sf file

names(zip_summary_sf)

Make a map of store counts - possibly?

mapview(zip_summary_sf, zcol = “store_count”, legend = TRUE)

Task 3

Join Zip Code and Health Facilities

#Read NYS Health Care data

nyc_health <- readr::read_csv(“NYS_Health_Facility.csv”, lazy = FALSE) str(nyc_health)

Merge food stores and Zip Code Data

nyc_health_care_centers <- base::merge(nyc_zip_sf, nyc_health, by.x = “ZIPCODE”, by.y = “Cooperator Zip Code”) names(nyc_health_care_centers)

Aggregate by Zip Code

health_summary_sf <- nyc_health_care_centers %>% group_by(ZIPCODE) %>% summarise(facility_count = n())

check the names of the sf file

names(health_summary_sf)

Make a map of health care facility counts - possibly?

mapview(zip_summary_sf, zcol = “facility_count”, legend = TRUE)

Task 4

Read the Census Tract Data

nycCensus <- sf::st_read(‘nyc_census_tracts.shp’, stringsAsFactors = FALSE) str(nycCensus)

names(nycCensus)

We must now assign borough names to each borough code so we can

have the data sorted by borough

nycCensus %<>% dplyr::mutate(cntyFIPS = case_when( COUNTYFP == ‘Bronx’ ~ ‘005’, COUNTYFP == ‘Brooklyn’ ~ ‘047’, COUNTYFP == ‘Manhattan’ ~ ‘061’, COUNTYFP == ‘Queens’ ~ ‘081’, COUNTYFP == ‘Staten Island’ ~ ‘085’), )

We used readLines to bypass the second line/row in the csv file.

You can also manually delete that line first, and run the read_csv or read.cvs.

acsData <- readLines(“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,)

Merge (JOIN) ACS data to the census tracts

join by attributes /columns

But first we must make sure the GEOID and the GEO_IDs match

acsData <- acsData %>% mutate(GEOID = stringr::str_sub(GEO_ID, -11, -1))

popData <- nycCensus %>% left_join(acsData, by = “GEOID”)

Merge (JOIN) ACS data to the census tracts

join by attributes /columns

verify the data

sum(popData$totPop)

str(popData)

st_crs(popData) popNYC <- sf::st_transform(popData, st_crs(nyc_covid_data_sf_merged))

Use JOINED zip code data from task 1.

Now aggregate to the zip code level

Join by locations with st_join (spatial join)

popNYC <- sf::st_join(nyc_covid_data_sf_merged, popNYC %>% sf::st_centroid(), # this essentially converts census tracts to points join = st_contains) %>% group_by(ZIPCODE, PO_NAME, POPULATION, COUNTY, COVID_CASE_COUNT, TOTAL_COVID_TESTS) %>% # use names(zpNYC) and names(popNYC) to see what we have summarise(totPop = sum(totPop), malePctg = sum(malePop)/totPop*100, # note the totPop is the newly calculated asianPop = sum(asianPop), blackPop = sum(blackPop), hispanicPop = sum(hispanicPop), whitePop = sum(whitePop))

popNYC %>% head()

Check and verify the data again

sum(popNYC$totPop, na.rm = T)