R Week 07 Assignment

Author

William Cornejo

Published

March 22, 2025

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

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

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"))

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)
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.

Health Facilities

Code
#mapview() + mapview(hf_sf)

Retail Stores

Code
#mapview() + mapview(r_sf)

Zip Codes

Code
mapview() + mapview(zip_codes['POPULATION'])

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

Code
save(zip_codes, zip_codes, 
     file = 'zip_codes_sf.RData')
save(hf_sf, health_facilities,
     file = 'health_facilities.RData')
# Now a geopackage
st_write(r_sf,          
         dsn = 'retail_stores.gpkg', 
         layer='',
         delete_layer = TRUE)
writing: substituting ENGCRS["Undefined Cartesian SRS with unknown unit"] for missing CRS
Deleting layer `' failed
Writing layer `' to data source `retail_stores.gpkg' using driver `GPKG'
Writing 23972 features with 16 fields and geometry type Point.