toc: true toc-location: left code-fold: true code-summary: “Show the code” code-tools: true

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:

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.

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

#Tasks for the first lab: #Set up a R project for the R-Spatial section. #Read the NYC postal areas in Shapefiles into sf objects. As NYC DOH publishes COVID-19 data by zip code, we will utilize the postal area data later.

NYCpostal_sf <- st_read("/Users/veronicawelsh/Desktop/gtech r/Week_07/R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `/Users/veronicawelsh/Desktop/gtech r/Week_07/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)

#Read and process the NYS health facilities spreadsheet data. Create sf objects from geographic coordinates.

nys_healthfac_data<-read.csv("/Users/veronicawelsh/Desktop/gtech r/Week_07/R-Spatial_I_Lab/NYS_Health_Facility.csv", stringsAsFactors = FALSE)
nys_healthfac_data %>% head()
##   Facility.ID                                  Facility.Name Short.Description
## 1         204                             Hospice at Lourdes              HSPC
## 2         620        Charles T Sitrin Health Care Center Inc                NH
## 3         654 Central Park Rehabilitation and Nursing Center                NH
## 4        1156                         East Side Nursing Home                NH
## 5        2589                   Wellsville Manor Care Center                NH
## 6        3455              Harris Hill Nursing Facility, LLC                NH
##                              Description Facility.Open.Date
## 1                                Hospice         06/01/1985
## 2 Residential Health Care Facility - SNF         02/01/1989
## 3 Residential Health Care Facility - SNF         02/01/1989
## 4 Residential Health Care Facility - SNF         08/01/1979
## 5 Residential Health Care Facility - SNF         02/01/1989
## 6 Residential Health Care Facility - SNF         04/08/1992
##            Facility.Address.1 Facility.Address.2 Facility.City Facility.State
## 1        4102 Old Vestal Road                           Vestal       New York
## 2          2050 Tilden Avenue                     New Hartford       New York
## 3 116 Martin Luther King East                         Syracuse       New York
## 4              62 Prospect St                           Warsaw       New York
## 5          4192A Bolivar Road                       Wellsville       New York
## 6           2699 Wehrle Drive                    Williamsville       New York
##   Facility.Zip.Code Facility.Phone.Number Facility.Fax.Number Facility.Website
## 1             13850            6077985692                  NA                 
## 2             13413            3157973114                  NA                 
## 3             13205            3154751641                  NA                 
## 4             14569            5857868151                  NA                 
## 5             14895            5855934400                  NA                 
## 6             14221            7166323700                  NA                 
##   Facility.County.Code Facility.County Regional.Office.ID
## 1                    3          Broome                  3
## 2                   32          Oneida                  3
## 3                   33        Onondaga                  3
## 4                   60         Wyoming                  1
## 5                    2        Allegany                  1
## 6                   14            Erie                  1
##                     Regional.Office Main.Site.Name Main.Site.Facility.ID
## 1  Central New York Regional Office                                   NA
## 2  Central New York Regional Office                                   NA
## 3  Central New York Regional Office                                   NA
## 4 Western Regional Office - Buffalo                                   NA
## 5 Western Regional Office - Buffalo                                   NA
## 6 Western Regional Office - Buffalo                                   NA
##   Operating.Certificate.Number                             Operator.Name
## 1                     0301501F Our Lady of Lourdes Memorial Hospital Inc
## 2                     3227304N  Charles T Sitrin Health Care Center, Inc
## 3                     3301326N                                CPRNC, LLC
## 4                     6027303N                East Side Nursing Home Inc
## 5                     0228305N                      Wellsville Manor LLC
## 6                     1406301N         Harris Hill Nursing Facility, LLC
##               Operator.Address.1 Operator.Address.2 Operator.City
## 1            169 Riverside Drive                       Binghamton
## 2         Box 1000 Tilden Avenue                     New Hartford
## 3    116 Martin Luther King East                         Syracuse
## 4             62 Prospect Street                           Warsaw
## 5             4192a Bolivar Road                       Wellsville
## 6 560 Delaware Avenue, Suite 400                          Buffalo
##   Operator.State Operator.Zip.Code Cooperator.Name Cooperator.Address
## 1       New York             13905                                   
## 2       New York             13413                                   
## 3       New York             13205                                   
## 4       New York             14569                                   
## 5       New York             14897                                   
## 6       New York             14202                                   
##   Cooperator.Address.2 Cooperator.City Cooperator.State Cooperator.Zip.Code
## 1                                              New York                  NA
## 2                                              New York                  NA
## 3                                              New York                  NA
## 4                                              New York                  NA
## 5                                              New York                  NA
## 6                                              New York                  NA
##               Ownership.Type Facility.Latitude Facility.Longitude
## 1 Not for Profit Corporation          42.09710          -75.97524
## 2 Not for Profit Corporation          43.05497          -75.22883
## 3                        LLC                NA                 NA
## 4       Business Corporation          42.73898          -78.12867
## 5                        LLC          42.12646          -77.96783
## 6                        LLC          42.95627          -78.68856
##         Facility.Location
## 1 (42.097095, -75.975243)
## 2  (43.05497, -75.228828)
## 3                        
## 4  (42.738979, -78.12867)
## 5 (42.126461, -77.967834)
## 6  (42.956268, -78.68856)
missing_latlong <- is.na(nys_healthfac_data$Facility.Longitude) | is.na(nys_healthfac_data$Facility.Latitude)

nys_healthfac_data_cleaned <- nys_healthfac_data[!missing_latlong, ]

nys_healthfac_sf <- st_as_sf(nys_healthfac_data_cleaned, coords = c("Facility.Longitude", "Facility.Latitude"))

st_crs(nys_healthfac_sf) <- 4326

mapview(nys_healthfac_sf)

#Read and process the NYS retail food stores data. Create sf objects from geographic coordinates for NYC.

nys_foodstore_data<-read.csv("/Users/veronicawelsh/Desktop/gtech r/Week_07/R-Spatial_I_Lab/NYS_Retail_Food_Stores.csv", stringsAsFactors = FALSE)
nys_foodstore_data %>% head()
##   County License.Number Operation.Type Establishment.Type
## 1 Albany         733149          Store             A     
## 2 Albany         704590          Store             JAC   
## 3 Albany         727909          Store             JAC   
## 4 Albany         720557          Store             JAC   
## 5 Albany          15890          Store             A     
## 6 Albany         735254          Store             JAC   
##                         Entity.Name                DBA.Name Street.Number
## 1 SPEEDWAY LLC                      12110                             719
## 2 1250 SELKIRK INC                  1250 SELKIRK                     1250
## 3 RED-KAP SALES INC                 1667 GENERAL STORE               1667
## 4 SAEED SADIQ, SAIKA NOREEN         19 STREET QUICK STOP              315
## 5 AZIZ MOHAMMAD S                   24 HR ALBANY NEWS                   8
## 6 7-ELEVEN INC                      7-ELEVEN                          477
##                       Street.Name Address.Line.2 Address.Line.3
## 1   NEW LOUDON RD                             NA             NA
## 2    RTE 9W & 396                             NA             NA
## 3    WESTERN AVENUE                           NA             NA
## 4   19TH STREET                               NA             NA
## 5 CENTRAL AVE                                 NA             NA
## 6   DELAWARE AVE                              NA             NA
##                 City State Zip.Code Square.Footage
## 1 LATHAM                NY    12110            300
## 2 SELKIRK               NY    12158          3,000
## 3 ALBANY                NY    12203          2,000
## 4 WATERVLIET            NY    12189          1,200
## 5 ALBANY                NY    12210          1,800
## 6 ALBANY                NY    12209              0
##                                                         Location
## 1   719 NEW LOUDON RD\nLATHAM, NY 12110\n(42.739618, -73.761949)
## 2         1250 RTE 9 W\nSELKIRK, NY 12158\n(42.547591, -73.8073)
## 3 1667 WESTERN AVENUE\nALBANY, NY 12203\n(42.686553, -73.854665)
## 4  315 19TH STREET\nWATERVLIET, NY 12189\n(42.73063, -73.703443)
## 5       8 CENTRAL AVE\nALBANY, NY 12210\n(42.657136, -73.763712)
## 6    477 DELAWARE AVE\nALBANY, NY 12209\n(42.639931, -73.784962)
leftPos<-stringr::str_locate(nys_foodstore_data$Location, "\\(")[,1]
rghtPos<-stringr::str_locate(nys_foodstore_data$Location, "\\)")[,1]

nys_foodstore_data$Location %>% stringr::str_sub(leftPos+1, rghtPos -1) -> nys_foodstore_data$Coords

cmmaPos<-stringr::str_locate(nys_foodstore_data$Coords, ", ")

nys_foodstore_data$Y<-stringr::str_sub(nys_foodstore_data$Coords, 1, cmmaPos[,1]-1) %>% as.numeric()
## Warning in stringr::str_sub(nys_foodstore_data$Coords, 1, cmmaPos[, 1] - : NAs
## introduced by coercion
nys_foodstore_data$X<-stringr::str_sub(nys_foodstore_data$Coords, cmmaPos[,2]+1) %>% as.numeric()
## Warning in stringr::str_sub(nys_foodstore_data$Coords, cmmaPos[, 2] + 1) %>% :
## NAs introduced by coercion
nys_foodstore_sf<-st_as_sf(nys_foodstore_data %>% tidyr::drop_na(X, Y), coords = c('X', 'Y'))

st_crs(nys_foodstore_sf) <- 4326
mapview(nys_foodstore_sf)

#Save the three sf objects in a RData file or in a single GeoPackage file/database.

save(NYCpostal_sf, nys_healthfac_sf, nys_foodstore_sf, file = "spatial_dataWeek7VeronicaWelsh.RData")