R Week 07 Assignment

Author

Caitlin Cacciatore

Published

March 12, 2026

install.packages(‘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.

R Spatial Lab Assignment # 1

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)

ZIP CODE DATA CODE

read the file

zip_sf <- st_read(“Zip_Code_040114.shp”)

#clean the data

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

nyc_zip_sf <- st_as_sf(nyc_zip,crs = 4326)

create fancy plots

plot(nyc_zip_sf)

Health Facilities Code

read the file

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

checking structure

str(NY_Health_sf)

#clean the data

NY_Health_df_clean <- NY_Health_df_clean %>% 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))

plot(st_geometry(NY_Health_sf[“Facility Zip Code”]), xlim = c(-80, -71), ylim = c(40, 46))

RETAIL FOOD STORES CODE

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

Remove rows with those missing coordinates

nys_clean_data <- nys_clean_data %>% 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"