── 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
Show the code
#Loading the Packagesoptions(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 installedlist.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
Reading layer `ZIP_CODE_040114' from data source
`/Users/heliosselene/Desktop/R-Spatial/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)
Warning: plotting the first 9 out of 12 attributes; use max.plot = 12 to plot
all
Show the code
# figure out structurestr(nyc_zip_sf)
Classes 'sf' and 'data.frame': 263 obs. of 13 variables:
$ ZIPCODE : chr "11436" "11213" "11212" "11225" ...
$ BLDGZIP : chr "0" "0" "0" "0" ...
$ PO_NAME : chr "Jamaica" "Brooklyn" "Brooklyn" "Brooklyn" ...
$ POPULATION: num 18681 62426 83866 56527 72280 ...
$ AREA : num 22699295 29631004 41972104 23698630 36868799 ...
$ STATE : chr "NY" "NY" "NY" "NY" ...
$ COUNTY : chr "Queens" "Kings" "Kings" "Kings" ...
$ ST_FIPS : chr "36" "36" "36" "36" ...
$ CTY_FIPS : chr "081" "047" "047" "047" ...
$ URL : chr "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" ...
$ SHAPE_AREA: num 0 0 0 0 0 0 0 0 0 0 ...
$ SHAPE_LEN : num 0 0 0 0 0 0 0 0 0 0 ...
$ geometry :sfc_POLYGON of length 263; first list element: List of 1
..$ : num [1:159, 1:2] 1038098 1038142 1038171 1038280 1038521 ...
..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "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:12] "ZIPCODE" "BLDGZIP" "PO_NAME" "POPULATION" ...
Show the code
# Read the COVID data for one week covid_data <- readr::read_csv("tests-by-zcta_2021_04_23.csv", lazy =FALSE)
Rows: 177 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): NEIGHBORHOOD_NAME, BOROUGH_GROUP, label
dbl (10): MODIFIED_ZCTA, lat, lon, COVID_CASE_COUNT, COVID_CASE_RATE, POP_DE...
ℹ 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.
Rows: 29389 Columns: 15
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (11): County, License Number, Operation Type, Establishment Type, Entity...
dbl (1): Zip Code
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.
Show the code
str(nys_retail)
spc_tbl_ [29,389 × 15] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ County : chr [1:29389] "Albany" "Albany" "Albany" "Albany" ...
$ License Number : chr [1:29389] "733149" "704590" "727909" "720557" ...
$ Operation Type : chr [1:29389] "Store" "Store" "Store" "Store" ...
$ Establishment Type: chr [1:29389] "A" "JAC" "JAC" "JAC" ...
$ Entity Name : chr [1:29389] "SPEEDWAY LLC" "1250 SELKIRK INC" "RED-KAP SALES INC" "SAEED SADIQ, SAIKA NOREEN" ...
$ DBA Name : chr [1:29389] "12110" "1250 SELKIRK" "1667 GENERAL STORE" "19 STREET QUICK STOP" ...
$ Street Number : chr [1:29389] "719" "1250" "1667" "315" ...
$ Street Name : chr [1:29389] "NEW LOUDON RD" "RTE 9W & 396" "WESTERN AVENUE" "19TH STREET" ...
$ Address Line 2 : logi [1:29389] NA NA NA NA NA NA ...
$ Address Line 3 : logi [1:29389] NA NA NA NA NA NA ...
$ City : chr [1:29389] "LATHAM" "SELKIRK" "ALBANY" "WATERVLIET" ...
$ State : chr [1:29389] "NY" "NY" "NY" "NY" ...
$ Zip Code : num [1:29389] 12110 12158 12203 12189 12210 ...
$ Square Footage : num [1:29389] 300 3000 2000 1200 1800 0 0 200 0 2000 ...
$ Location : chr [1:29389] "719 NEW LOUDON RD\nLATHAM, NY 12110\n(42.739618, -73.761949)" "1250 RTE 9 W\nSELKIRK, NY 12158\n(42.547591, -73.8073)" "1667 WESTERN AVENUE\nALBANY, NY 12203\n(42.686553, -73.854665)" "315 19TH STREET\nWATERVLIET, NY 12189\n(42.73063, -73.703443)" ...
- attr(*, "spec")=
.. cols(
.. County = col_character(),
.. `License Number` = col_character(),
.. `Operation Type` = col_character(),
.. `Establishment Type` = col_character(),
.. `Entity Name` = col_character(),
.. `DBA Name` = col_character(),
.. `Street Number` = col_character(),
.. `Street Name` = col_character(),
.. `Address Line 2` = col_logical(),
.. `Address Line 3` = col_logical(),
.. City = col_character(),
.. State = col_character(),
.. `Zip Code` = col_double(),
.. `Square Footage` = col_number(),
.. Location = col_character()
.. )
- attr(*, "problems")=<externalptr>
Show the code
# Merge food stores and Zip Code Datanyc_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 filenames(zip_summary_sf)
[1] "ZIPCODE" "store_count" "geometry"
Task 3
Show the code
# Task 3# Join Zip Code and Health Facilities#Read NYS Health Care datanyc_health <- readr::read_csv("NYS_Health_Facility.csv", lazy =FALSE)
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
str(nyc_health)
spc_tbl_ [3,990 × 36] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
$ Facility ID : num [1:3990] 204 620 654 1156 2589 ...
$ Facility Name : chr [1:3990] "Hospice at Lourdes" "Charles T Sitrin Health Care Center Inc" "Central Park Rehabilitation and Nursing Center" "East Side Nursing Home" ...
$ Short Description : chr [1:3990] "HSPC" "NH" "NH" "NH" ...
$ Description : chr [1:3990] "Hospice" "Residential Health Care Facility - SNF" "Residential Health Care Facility - SNF" "Residential Health Care Facility - SNF" ...
$ Facility Open Date : chr [1:3990] "06/01/1985" "02/01/1989" "02/01/1989" "08/01/1979" ...
$ Facility Address 1 : chr [1:3990] "4102 Old Vestal Road" "2050 Tilden Avenue" "116 Martin Luther King East" "62 Prospect St" ...
$ Facility Address 2 : chr [1:3990] NA NA NA NA ...
$ Facility City : chr [1:3990] "Vestal" "New Hartford" "Syracuse" "Warsaw" ...
$ Facility State : chr [1:3990] "New York" "New York" "New York" "New York" ...
$ Facility Zip Code : chr [1:3990] "13850" "13413" "13205" "14569" ...
$ Facility Phone Number : num [1:3990] 6.08e+09 3.16e+09 3.15e+09 5.86e+09 5.86e+09 ...
$ Facility Fax Number : num [1:3990] NA NA NA NA NA ...
$ Facility Website : chr [1:3990] NA NA NA NA ...
$ Facility County Code : num [1:3990] 3 32 33 60 2 ...
$ Facility County : chr [1:3990] "Broome" "Oneida" "Onondaga" "Wyoming" ...
$ Regional Office ID : num [1:3990] 3 3 3 1 1 1 7 1 7 5 ...
$ Regional Office : chr [1:3990] "Central New York Regional Office" "Central New York Regional Office" "Central New York Regional Office" "Western Regional Office - Buffalo" ...
$ Main Site Name : chr [1:3990] NA NA NA NA ...
$ Main Site Facility ID : num [1:3990] NA NA NA NA NA ...
$ Operating Certificate Number: chr [1:3990] "0301501F" "3227304N" "3301326N" "6027303N" ...
$ Operator Name : chr [1:3990] "Our Lady of Lourdes Memorial Hospital Inc" "Charles T Sitrin Health Care Center, Inc" "CPRNC, LLC" "East Side Nursing Home Inc" ...
$ Operator Address 1 : chr [1:3990] "169 Riverside Drive" "Box 1000 Tilden Avenue" "116 Martin Luther King East" "62 Prospect Street" ...
$ Operator Address 2 : chr [1:3990] NA NA NA NA ...
$ Operator City : chr [1:3990] "Binghamton" "New Hartford" "Syracuse" "Warsaw" ...
$ Operator State : chr [1:3990] "New York" "New York" "New York" "New York" ...
$ Operator Zip Code : chr [1:3990] "13905" "13413" "13205" "14569" ...
$ Cooperator Name : chr [1:3990] NA NA NA NA ...
$ Cooperator Address : chr [1:3990] NA NA NA NA ...
$ Cooperator Address 2 : chr [1:3990] NA NA NA NA ...
$ Cooperator City : chr [1:3990] NA NA NA NA ...
$ Cooperator State : chr [1:3990] "New York" "New York" "New York" "New York" ...
$ Cooperator Zip Code : chr [1:3990] NA NA NA NA ...
$ Ownership Type : chr [1:3990] "Not for Profit Corporation" "Not for Profit Corporation" "LLC" "Business Corporation" ...
$ Facility Latitude : num [1:3990] 42.1 43.1 NA 42.7 42.1 ...
$ Facility Longitude : num [1:3990] -76 -75.2 NA -78.1 -78 ...
$ Facility Location : chr [1:3990] "(42.097095, -75.975243)" "(43.05497, -75.228828)" NA "(42.738979, -78.12867)" ...
- attr(*, "spec")=
.. cols(
.. `Facility ID` = col_double(),
.. `Facility Name` = col_character(),
.. `Short Description` = col_character(),
.. Description = col_character(),
.. `Facility Open Date` = col_character(),
.. `Facility Address 1` = col_character(),
.. `Facility Address 2` = col_character(),
.. `Facility City` = col_character(),
.. `Facility State` = col_character(),
.. `Facility Zip Code` = col_character(),
.. `Facility Phone Number` = col_double(),
.. `Facility Fax Number` = col_double(),
.. `Facility Website` = col_character(),
.. `Facility County Code` = col_double(),
.. `Facility County` = col_character(),
.. `Regional Office ID` = col_double(),
.. `Regional Office` = col_character(),
.. `Main Site Name` = col_character(),
.. `Main Site Facility ID` = col_double(),
.. `Operating Certificate Number` = col_character(),
.. `Operator Name` = col_character(),
.. `Operator Address 1` = col_character(),
.. `Operator Address 2` = col_character(),
.. `Operator City` = col_character(),
.. `Operator State` = col_character(),
.. `Operator Zip Code` = col_character(),
.. `Cooperator Name` = col_character(),
.. `Cooperator Address` = col_character(),
.. `Cooperator Address 2` = col_character(),
.. `Cooperator City` = col_character(),
.. `Cooperator State` = col_character(),
.. `Cooperator Zip Code` = col_character(),
.. `Ownership Type` = col_character(),
.. `Facility Latitude` = col_double(),
.. `Facility Longitude` = col_double(),
.. `Facility Location` = col_character()
.. )
- attr(*, "problems")=<externalptr>
Show the code
# Merge food stores and Zip Code Datanyc_health_care_centers <- base::merge(nyc_zip_sf, nyc_health, by.x ="ZIPCODE", by.y ="Cooperator Zip Code")names(nyc_health_care_centers)
# We must now assign borough names to each borough code so we can # have the data sorted by boroughnycCensus %<>% dplyr::mutate(cntyFIPS =case_when( COUNTYFP =='Bronx'~'005', COUNTYFP =='Brooklyn'~'047', COUNTYFP =='Manhattan'~'061', COUNTYFP =='Queens'~'081', COUNTYFP =='Staten Island'~'085'),)
Tasks 4 and 5
#I could Not Get the Following Lines to Work and/or Render after many hours of attempts.
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)
<!-- -->
::: {.quarto-embedded-source-code}
```````````````````{.markdown shortcodes="false"}
---
title: "R Week 08 Assignment"
author: "Caitlin Cacciatore"
date: "3/20/2026"
format:
html:
toc: true
toc-location: left
code-fold: true
code-summary: "Show the code"
code-tools: true
---
```{r load_packages, include=TRUE}
require(tidyverse);
require(sf);
require(mapview);
require(magrittr)
#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)
# Load all packages.
lapply(list.of.packages,function(x) {
require(x,character.only = TRUE,quietly = TRUE)
})
install.packages("sf") # run once if not installed
install.packages("tidyverse")
library(sf)
library(tidyverse)
```{r Working Code, include=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'),
)
Tasks 4 and 5
#I could Not Get the Following Lines to Work and/or Render after many hours of attempts.
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))