R Week 07 Assignment

Author

William Cornejo

Published

March 22, 2025

GTECH 78520 HW 7

R Spatial Lab Assignment # 1

task 1: Set up R Project

Code
smpCode <- "hello, R markdown and RPubs!"

cat(smpCode)
hello, R markdown and RPubs!

task 2: read zip code shapefile

Code
zip_codes <- st_read('R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp')
Reading layer `ZIP_CODE_040114' from data source 
  `C:\Users\wcornejo\Documents\assign7\assign7\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)

task 3: read and process the nys health facilities spreadsheet data (.csv file)

Code
health_facilities <- read.csv('R-Spatial_I_Lab/NYS_Health_Facility 3(in).csv')
hf_df <- health_facilities[!is.na(health_facilities$Facility.Longitude), ]
hf_df <- hf_df[!is.na(hf_df$Facility.Latitude), ]
hf_df <- hf_df[!is.na(hf_df$Facility.Longitude), ]
hf_df <- hf_df[hf_df$Facility.Latitude != 0, ]
hf_df <- hf_df[hf_df$Facility.Longitude != 0, ]
hf_sf <- st_as_sf(hf_df, coords = c("Facility.Longitude", "Facility.Latitude"))
plot(hf_sf, main='Health Facilities Data')
Warning: plotting the first 9 out of 34 attributes; use max.plot = 34 to plot
all

task 4: read and process the nys retail food stores data

Code
retail_df <- read.csv('R-Spatial_I_Lab/NYS_Retail_Food_Stores.csv')
retail_df_xy <- read.csv('R-Spatial_I_Lab/nys_retail_food_store_xy.csv')
r_sf <- retail_df_xy[!is.na(retail_df_xy$X),]
r_sf <- r_sf[!is.na(r_sf$Y),]
r_sf <- st_as_sf(r_sf, coords = c("X", "Y"))
#had to save with encoding utf-8 in vscode
plot(r_sf, main = 'Retail Food Stores in NYS Data')
Warning: plotting the first 9 out of 16 attributes; use max.plot = 16 to plot
all
Warning in min(x): no non-missing arguments to min; returning Inf
Warning in max(x): no non-missing arguments to max; returning -Inf

task 5: Use simple mapping method such as mapview with a basemap to verify the above datasets in terms of their geometry locations.

One zip code

Code
one_zip <- zip_codes %>%
  filter(ZIPCODE == '11421')
mapview(one_zip)

Health Facilities in King’s County

The mapview does not format correctly so i took a screenshot of the render, which worked fine. It’s just the publish that doesn’t work.

Code
brooklyn_hf <- hf_sf %>%
  filter( Facility.County== 'Kings')
#mapview() + mapview(brooklyn_hf)

Alt text

Retail Stores in zip code 11421

Code
r_sf_filt <- r_sf %>%
  filter(Zip.Code == '11421')
mapview() + mapview(r_sf_filt)

task 6: Save the three sf objects in a RData file or in a single GeoPackage file/database.

Code
# Now a geopackage
save(r_sf, hf_sf, zip_codes, 
     file = 'lab7.RData')