R Week 07 Assignment

Author

Caitlin Cacciatore

Published

March 12, 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//Rtmpt2Ly0E/downloaded_packages
Show the code
install.packages("tidyverse")

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

ZIP CODE DATA CODE

Show the code
# read the file 

zip_sf <- st_read("Zip_Code_040114.shp")
Reading layer `ZIP_CODE_040114' from data source 
  `/Users/heliosselene/Desktop/Hunter/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)
Show the code
#clean the data

zip_nyc <- zip_sf %>%
  filter(!is.na("Zip Code"))

nyc_zip_sf <- st_as_sf(zip_nyc,crs = 4326)

# create  fancy plots

plot(nyc_zip_sf)
Warning: plotting the first 9 out of 12 attributes; use max.plot = 12 to plot
all

Health Facilities Code

Show the code
# read the file


NY_Health_df <- read_csv("NYS_Health_Facility.csv", 
                               show_col_types = FALSE, 
                               lazy = FALSE) 

# checking structure
                               
str(NY_Health_df)
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
#clean the data


NY_Health_df_clean <- NY_Health_df %>%
  filter(!is.na(`Facility Longitude`) & !is.na(`Facility Latitude`))

# Convert to sf


library(sf)

NY_Health_sf <- st_as_sf(
  NY_Health_df_clean,
  coords = c("Facility Longitude", "Facility Latitude"),
  crs = 4326
)

# create a nice little (simple) plot

plot(st_geometry(NY_Health_sf),xlim=c(-70,-80),ylim=c(40,45))

Show the code
plot(st_geometry(NY_Health_sf["Facility Zip Code"]),
     xlim = c(-80, -71),
     ylim = c(40, 46))

RETAIL FOOD STORES CODE

I could not get this part to render despite an hour of attempts. Please forgive

that this is not in code format; and please advise on how I could render it

in the future. Many thanks.

install.packages(“janitor”) library(janitor)

nys_retail_food_store_xy_df <- read_csv(“nys_retail_food_store_xy.csv”, show_col_types = FALSE, lazy = FALSE) %>%

str(nys_retail_food_store_xy_df)

Remove rows with those missing coordinates

nys_clean_data <- nys_retail_food_store_xy_df %>% filter(!is.na(“x”) & !is.na(“y”))

Convert to sf object (longitude first, latitude second)

nys_retail_food_store_xy_sf <- st_as_sf( nys_clean_data, coords = c(“x”, “y”), # X = longitude, Y = latitude crs = 4326 )

#check structure

str(nys_retail_food_store_xy_sf)

make a plot

plot(st_geometry(nys_retail_food_store_xy_sf))

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

You can name the code chunk and also set options.

task 1:

Show the code
smpCode <- "hello, R markdown and RPubs!"

cat(smpCode)
hello, R markdown and RPubs!

task 2:

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

Show the code
print("This is the new code chunk options available in Quarto Markdown")
[1] "This is the new code chunk options available in Quarto Markdown"