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 - Week 7

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

task 1: Set up a R project for the R-Spatial section.

# setting up R project for R-Spatial section

dir.create("~/Documents/R-Spatial", recursive = TRUE)
## Warning in dir.create("~/Documents/R-Spatial", recursive = TRUE):
## '/Users/samikarim/Documents/R-Spatial' already exists
setwd("~/Documents/R-Spatial")

task 2: 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.

# read shapefile into sf object

nyc_zip_sf <- st_read("data/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `/Users/samikarim/Documents/R-spatial/data/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)
str(nyc_zip_sf)
## Classes 'sf' and 'data.frame':   263 obs. of  13 variables:
##  $ ZIPCODE   : chr  "11436" "11213" "11212" "11225" ...
##  $ BLDGZIP   : chr  "0" "0" "0" "0" ...
##  $ PO_NAME   : chr  "Jamaica" "Brooklyn" "Brooklyn" "Brooklyn" ...
##  $ POPULATION: num  18681 62426 83866 56527 72280 ...
##  $ AREA      : num  22699295 29631004 41972104 23698630 36868799 ...
##  $ STATE     : chr  "NY" "NY" "NY" "NY" ...
##  $ COUNTY    : chr  "Queens" "Kings" "Kings" "Kings" ...
##  $ ST_FIPS   : chr  "36" "36" "36" "36" ...
##  $ CTY_FIPS  : chr  "081" "047" "047" "047" ...
##  $ URL       : chr  "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" ...
##  $ SHAPE_AREA: num  0 0 0 0 0 0 0 0 0 0 ...
##  $ SHAPE_LEN : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ geometry  :sfc_POLYGON of length 263; first list element: List of 1
##   ..$ : num [1:159, 1:2] 1038098 1038142 1038171 1038280 1038521 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:12] "ZIPCODE" "BLDGZIP" "PO_NAME" "POPULATION" ...

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

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

# read csv into df 

nyshealth_df <- read_csv("data/NYS_Health_Facility.csv", 
                               show_col_types = FALSE, 
                               lazy = FALSE)
str(nyshealth_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>
# filter out NA from dataframe 

sum(is.na(nyshealth_df$`Facility Longitude`))
## [1] 142
sum(is.na(nyshealth_df$`Facility Latitude`))
## [1] 142
nyshealth_df_ <- nyshealth_df %>%
  dplyr::filter(
    !is.na(`Facility Longitude`),
    !is.na(`Facility Latitude`)
  )

# create sf object with geographic coordinates 

nyshealth_sf <- st_as_sf(nyshealth_df_, 
                               coords = c("Facility Longitude", "Facility Latitude"))
str(nyshealth_sf)
## sf [3,848 × 35] (S3: sf/tbl_df/tbl/data.frame)
##  $ Facility ID                 : num [1:3848] 204 620 1156 2589 3455 ...
##  $ Facility Name               : chr [1:3848] "Hospice at Lourdes" "Charles T Sitrin Health Care Center Inc" "East Side Nursing Home" "Wellsville Manor Care Center" ...
##  $ Short Description           : chr [1:3848] "HSPC" "NH" "NH" "NH" ...
##  $ Description                 : chr [1:3848] "Hospice" "Residential Health Care Facility - SNF" "Residential Health Care Facility - SNF" "Residential Health Care Facility - SNF" ...
##  $ Facility Open Date          : chr [1:3848] "06/01/1985" "02/01/1989" "08/01/1979" "02/01/1989" ...
##  $ Facility Address 1          : chr [1:3848] "4102 Old Vestal Road" "2050 Tilden Avenue" "62 Prospect St" "4192A Bolivar Road" ...
##  $ Facility Address 2          : chr [1:3848] NA NA NA NA ...
##  $ Facility City               : chr [1:3848] "Vestal" "New Hartford" "Warsaw" "Wellsville" ...
##  $ Facility State              : chr [1:3848] "New York" "New York" "New York" "New York" ...
##  $ Facility Zip Code           : chr [1:3848] "13850" "13413" "14569" "14895" ...
##  $ Facility Phone Number       : num [1:3848] 6.08e+09 3.16e+09 5.86e+09 5.86e+09 7.17e+09 ...
##  $ Facility Fax Number         : num [1:3848] NA NA NA NA NA ...
##  $ Facility Website            : chr [1:3848] NA NA NA NA ...
##  $ Facility County Code        : num [1:3848] 3 32 60 2 14 ...
##  $ Facility County             : chr [1:3848] "Broome" "Oneida" "Wyoming" "Allegany" ...
##  $ Regional Office ID          : num [1:3848] 3 3 1 1 1 7 1 7 5 7 ...
##  $ Regional Office             : chr [1:3848] "Central New York Regional Office" "Central New York Regional Office" "Western Regional Office - Buffalo" "Western Regional Office - Buffalo" ...
##  $ Main Site Name              : chr [1:3848] NA NA NA NA ...
##  $ Main Site Facility ID       : num [1:3848] NA NA NA NA NA ...
##  $ Operating Certificate Number: chr [1:3848] "0301501F" "3227304N" "6027303N" "0228305N" ...
##  $ Operator Name               : chr [1:3848] "Our Lady of Lourdes Memorial Hospital Inc" "Charles T Sitrin Health Care Center, Inc" "East Side Nursing Home Inc" "Wellsville Manor LLC" ...
##  $ Operator Address 1          : chr [1:3848] "169 Riverside Drive" "Box 1000 Tilden Avenue" "62 Prospect Street" "4192a Bolivar Road" ...
##  $ Operator Address 2          : chr [1:3848] NA NA NA NA ...
##  $ Operator City               : chr [1:3848] "Binghamton" "New Hartford" "Warsaw" "Wellsville" ...
##  $ Operator State              : chr [1:3848] "New York" "New York" "New York" "New York" ...
##  $ Operator Zip Code           : chr [1:3848] "13905" "13413" "14569" "14897" ...
##  $ Cooperator Name             : chr [1:3848] NA NA NA NA ...
##  $ Cooperator Address          : chr [1:3848] NA NA NA NA ...
##  $ Cooperator Address 2        : chr [1:3848] NA NA NA NA ...
##  $ Cooperator City             : chr [1:3848] NA NA NA NA ...
##  $ Cooperator State            : chr [1:3848] "New York" "New York" "New York" "New York" ...
##  $ Cooperator Zip Code         : chr [1:3848] NA NA NA NA ...
##  $ Ownership Type              : chr [1:3848] "Not for Profit Corporation" "Not for Profit Corporation" "Business Corporation" "LLC" ...
##  $ Facility Location           : chr [1:3848] "(42.097095, -75.975243)" "(43.05497, -75.228828)" "(42.738979, -78.12867)" "(42.126461, -77.967834)" ...
##  $ geometry                    :sfc_POINT of length 3848; first list element:  'XY' num [1:2] -76 42.1
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:34] "Facility ID" "Facility Name" "Short Description" "Description" ...
# set coordinate reference system

st_crs(nyshealth_sf) <- 4326
st_crs(nyshealth_sf)
## Coordinate Reference System:
##   User input: EPSG:4326 
##   wkt:
## GEOGCRS["WGS 84",
##     ENSEMBLE["World Geodetic System 1984 ensemble",
##         MEMBER["World Geodetic System 1984 (Transit)"],
##         MEMBER["World Geodetic System 1984 (G730)"],
##         MEMBER["World Geodetic System 1984 (G873)"],
##         MEMBER["World Geodetic System 1984 (G1150)"],
##         MEMBER["World Geodetic System 1984 (G1674)"],
##         MEMBER["World Geodetic System 1984 (G1762)"],
##         MEMBER["World Geodetic System 1984 (G2139)"],
##         MEMBER["World Geodetic System 1984 (G2296)"],
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]],
##         ENSEMBLEACCURACY[2.0]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     USAGE[
##         SCOPE["Horizontal component of 3D system."],
##         AREA["World."],
##         BBOX[-90,-180,90,180]],
##     ID["EPSG",4326]]
nyshealth_sf
## Simple feature collection with 3848 features and 34 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -79.6299 ymin: -75.45935 xmax: 43.21162 ymax: 44.97849
## Geodetic CRS:  WGS 84
## # A tibble: 3,848 × 35
##    `Facility ID` `Facility Name`                 `Short Description` Description
##  *         <dbl> <chr>                           <chr>               <chr>      
##  1           204 Hospice at Lourdes              HSPC                Hospice    
##  2           620 Charles T Sitrin Health Care C… NH                  Residentia…
##  3          1156 East Side Nursing Home          NH                  Residentia…
##  4          2589 Wellsville Manor Care Center    NH                  Residentia…
##  5          3455 Harris Hill Nursing Facility, … NH                  Residentia…
##  6          3853 Garden City Surgi Center        DTC                 Diagnostic…
##  7          4249 Willcare                        CHHA                Certified …
##  8          4473 Good Shepherd Hospice           HSPC                Hospice    
##  9          6230 NYU Langone Rutherford          HOSP-EC             Hospital E…
## 10          6482 Endoscopy Center of Long Islan… DTC                 Diagnostic…
## # ℹ 3,838 more rows
## # ℹ 31 more variables: `Facility Open Date` <chr>, `Facility Address 1` <chr>,
## #   `Facility Address 2` <chr>, `Facility City` <chr>, `Facility State` <chr>,
## #   `Facility Zip Code` <chr>, `Facility Phone Number` <dbl>,
## #   `Facility Fax Number` <dbl>, `Facility Website` <chr>,
## #   `Facility County Code` <dbl>, `Facility County` <chr>,
## #   `Regional Office ID` <dbl>, `Regional Office` <chr>, …

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

# read csv into df 

nysretailfood_df <- read_csv("data/NYS_Retail_Food_Stores.csv", 
                               show_col_types = FALSE, 
                               lazy = FALSE)
str(nysretailfood_df)
## spc_tbl_ [29,389 × 15] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ County            : chr [1:29389] "Albany" "Albany" "Albany" "Albany" ...
##  $ License Number    : chr [1:29389] "733149" "704590" "727909" "720557" ...
##  $ Operation Type    : chr [1:29389] "Store" "Store" "Store" "Store" ...
##  $ Establishment Type: chr [1:29389] "A" "JAC" "JAC" "JAC" ...
##  $ Entity Name       : chr [1:29389] "SPEEDWAY LLC" "1250 SELKIRK INC" "RED-KAP SALES INC" "SAEED SADIQ, SAIKA NOREEN" ...
##  $ DBA Name          : chr [1:29389] "12110" "1250 SELKIRK" "1667 GENERAL STORE" "19 STREET QUICK STOP" ...
##  $ Street Number     : chr [1:29389] "719" "1250" "1667" "315" ...
##  $ Street Name       : chr [1:29389] "NEW LOUDON RD" "RTE 9W & 396" "WESTERN AVENUE" "19TH STREET" ...
##  $ Address Line 2    : logi [1:29389] NA NA NA NA NA NA ...
##  $ Address Line 3    : logi [1:29389] NA NA NA NA NA NA ...
##  $ City              : chr [1:29389] "LATHAM" "SELKIRK" "ALBANY" "WATERVLIET" ...
##  $ State             : chr [1:29389] "NY" "NY" "NY" "NY" ...
##  $ Zip Code          : num [1:29389] 12110 12158 12203 12189 12210 ...
##  $ Square Footage    : num [1:29389] 300 3000 2000 1200 1800 0 0 200 0 2000 ...
##  $ Location          : chr [1:29389] "719 NEW LOUDON RD\nLATHAM, NY 12110\n(42.739618, -73.761949)" "1250 RTE 9 W\nSELKIRK, NY 12158\n(42.547591, -73.8073)" "1667 WESTERN AVENUE\nALBANY, NY 12203\n(42.686553, -73.854665)" "315 19TH STREET\nWATERVLIET, NY 12189\n(42.73063, -73.703443)" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   County = col_character(),
##   ..   `License Number` = col_character(),
##   ..   `Operation Type` = col_character(),
##   ..   `Establishment Type` = col_character(),
##   ..   `Entity Name` = col_character(),
##   ..   `DBA Name` = col_character(),
##   ..   `Street Number` = col_character(),
##   ..   `Street Name` = col_character(),
##   ..   `Address Line 2` = col_logical(),
##   ..   `Address Line 3` = col_logical(),
##   ..   City = col_character(),
##   ..   State = col_character(),
##   ..   `Zip Code` = col_double(),
##   ..   `Square Footage` = col_number(),
##   ..   Location = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
# has no coordinates, read xy csv into df 

nysretailfood_xy_df <- read.csv("data/nys_retail_food_store_xy.csv",fileEncoding='latin1', check.names=F)

str(nysretailfood_xy_df)
## 'data.frame':    29389 obs. of  18 variables:
##  $ ï..County         : chr  "Albany" "Albany" "Albany" "Albany" ...
##  $ License.Number    : int  733149 704590 727909 720557 15890 735254 708848 716318 713889 715759 ...
##  $ Operation.Type    : chr  "Store" "Store" "Store" "Store" ...
##  $ Establishment.Type: chr  "A     " "JAC   " "JAC   " "JAC   " ...
##  $ Entity.Name       : chr  "SPEEDWAY LLC                     " "1250 SELKIRK INC                 " "RED-KAP SALES INC                " "SAEED SADIQ, SAIKA NOREEN        " ...
##  $ DBA.Name          : chr  "12110                  " "1250 SELKIRK           " "1667 GENERAL STORE     " "19 STREET QUICK STOP   " ...
##  $ Street.Number     : chr  "719" "1250" "1667" "315" ...
##  $ Street.Name       : chr  "NEW LOUDON RD                " "RTE 9W & 396                " "WESTERN AVENUE              " "19TH STREET                  " ...
##  $ Address.Line.2    : logi  NA NA NA NA NA NA ...
##  $ Address.Line.3    : logi  NA NA NA NA NA NA ...
##  $ City              : chr  "LATHAM            " "SELKIRK           " "ALBANY            " "WATERVLIET        " ...
##  $ State             : chr  "NY" "NY" "NY" "NY" ...
##  $ Zip.Code          : int  12110 12158 12203 12189 12210 12209 12110 12084 12077 12159 ...
##  $ Square.Footage    : chr  "300" "3,000" "2,000" "1,200" ...
##  $ Location          : chr  "719 NEW LOUDON RD\nLATHAM, NY 12110\n(42.739618, -73.761949)" "1250 RTE 9 W\nSELKIRK, NY 12158\n(42.547591, -73.8073)" "1667 WESTERN AVENUE\nALBANY, NY 12203\n(42.686553, -73.854665)" "315 19TH STREET\nWATERVLIET, NY 12189\n(42.73063, -73.703443)" ...
##  $ Coords            : chr  "42.739618, -73.761949" "42.547591, -73.8073" "42.686553, -73.854665" "42.73063, -73.703443" ...
##  $ Y                 : num  42.7 42.5 42.7 42.7 42.7 ...
##  $ X                 : num  -73.8 -73.8 -73.9 -73.7 -73.8 ...
# filter out NA from dataframe  

sum(is.na(nysretailfood_xy_df$`X`))
## [1] 5417
sum(is.na(nysretailfood_xy_df$`Y`))
## [1] 5417
nysretailfood_xy_df_ <- nysretailfood_xy_df %>%
  dplyr::filter(
    !is.na(`X`),
    !is.na(`Y`)
  )

# create sf object with geographic coordinates 

nysretailfood_sf <- st_as_sf(nysretailfood_xy_df_, 
                               coords = c("X", "Y"))
str(nysretailfood_sf)
## Classes 'sf' and 'data.frame':   23972 obs. of  17 variables:
##  $ ï..County         : chr  "Albany" "Albany" "Albany" "Albany" ...
##  $ License.Number    : int  733149 704590 727909 720557 15890 735254 708848 713889 715759 723927 ...
##  $ Operation.Type    : chr  "Store" "Store" "Store" "Store" ...
##  $ Establishment.Type: chr  "A     " "JAC   " "JAC   " "JAC   " ...
##  $ Entity.Name       : chr  "SPEEDWAY LLC                     " "1250 SELKIRK INC                 " "RED-KAP SALES INC                " "SAEED SADIQ, SAIKA NOREEN        " ...
##  $ DBA.Name          : chr  "12110                  " "1250 SELKIRK           " "1667 GENERAL STORE     " "19 STREET QUICK STOP   " ...
##  $ Street.Number     : chr  "719" "1250" "1667" "315" ...
##  $ Street.Name       : chr  "NEW LOUDON RD                " "RTE 9W & 396                " "WESTERN AVENUE              " "19TH STREET                  " ...
##  $ Address.Line.2    : logi  NA NA NA NA NA NA ...
##  $ Address.Line.3    : logi  NA NA NA NA NA NA ...
##  $ City              : chr  "LATHAM            " "SELKIRK           " "ALBANY            " "WATERVLIET        " ...
##  $ State             : chr  "NY" "NY" "NY" "NY" ...
##  $ Zip.Code          : int  12110 12158 12203 12189 12210 12209 12110 12077 12159 12084 ...
##  $ Square.Footage    : chr  "300" "3,000" "2,000" "1,200" ...
##  $ Location          : chr  "719 NEW LOUDON RD\nLATHAM, NY 12110\n(42.739618, -73.761949)" "1250 RTE 9 W\nSELKIRK, NY 12158\n(42.547591, -73.8073)" "1667 WESTERN AVENUE\nALBANY, NY 12203\n(42.686553, -73.854665)" "315 19TH STREET\nWATERVLIET, NY 12189\n(42.73063, -73.703443)" ...
##  $ Coords            : chr  "42.739618, -73.761949" "42.547591, -73.8073" "42.686553, -73.854665" "42.73063, -73.703443" ...
##  $ geometry          :sfc_POINT of length 23972; first list element:  'XY' num  -73.8 42.7
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:16] "ï..County" "License.Number" "Operation.Type" "Establishment.Type" ...
# set coordinate reference system to wgs 

st_crs(nysretailfood_sf) <- 4326
st_crs(nysretailfood_sf)
## Coordinate Reference System:
##   User input: EPSG:4326 
##   wkt:
## GEOGCRS["WGS 84",
##     ENSEMBLE["World Geodetic System 1984 ensemble",
##         MEMBER["World Geodetic System 1984 (Transit)"],
##         MEMBER["World Geodetic System 1984 (G730)"],
##         MEMBER["World Geodetic System 1984 (G873)"],
##         MEMBER["World Geodetic System 1984 (G1150)"],
##         MEMBER["World Geodetic System 1984 (G1674)"],
##         MEMBER["World Geodetic System 1984 (G1762)"],
##         MEMBER["World Geodetic System 1984 (G2139)"],
##         MEMBER["World Geodetic System 1984 (G2296)"],
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]],
##         ENSEMBLEACCURACY[2.0]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     USAGE[
##         SCOPE["Horizontal component of 3D system."],
##         AREA["World."],
##         BBOX[-90,-180,90,180]],
##     ID["EPSG",4326]]
nysretailfood_sf
## Simple feature collection with 23972 features and 16 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -79.75953 ymin: 40.50782 xmax: -71.93873 ymax: 44.99484
## Geodetic CRS:  WGS 84
## First 10 features:
##    ï..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   
## 7     Albany         708848          Store             JAC   
## 8     Albany         713889          Store             JAC   
## 9     Albany         715759          Store             JAC   
## 10    Albany         723927          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
## 7  ADVANCED FRESH CONCEPTS FRANCHISE AFC SUSHI @ PRICE CHOPP           873
## 8  ADVANCED FRESH CONCEPTS FRANCHISE AFC SUSHI @ PRICE CHOPP           329
## 9  ADVANCED FRESH CONCEPTS FRANCHISE AFC SUSHI @PRICE CHOPPE          1395
## 10 ADVANCED FRESH CONCEPTS FRAN CORP AFC SUSHI@ PRICE CHOPPE          2080
##                        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
## 7          NEW LOUDON RD                       NA             NA
## 8        GLENMONT RD                           NA             NA
## 9         NEW SCOTLAND RD                      NA             NA
## 10    WESTERN 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
## 7  LATHAM                NY    12110              0
## 8  GLENMONT              NY    12077              0
## 9  SLINGERLANDS          NY    12159          2,000
## 10 GUILDERLAND           NY    12084            400
##                                                                 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)
## 7            873 NEW LOUDON RD\nLATHAM, NY 12110\n(42.75459, -73.758714)
## 8           329 GLENMONT RD\nGLENMONT, NY 12077\n(42.601045, -73.790364)
## 9  1395 NEW SCOTLAND RD\nSLINGERLANDS, NY 12159\n(42.636568, -73.856367)
## 10       2080 WESTERN AVE\nGUILDERLAND, NY 12084\n(42.698502, -73.89159)
##                   Coords                   geometry
## 1  42.739618, -73.761949 POINT (-73.76195 42.73962)
## 2    42.547591, -73.8073  POINT (-73.8073 42.54759)
## 3  42.686553, -73.854665 POINT (-73.85466 42.68655)
## 4   42.73063, -73.703443 POINT (-73.70344 42.73063)
## 5  42.657136, -73.763712 POINT (-73.76371 42.65714)
## 6  42.639931, -73.784962 POINT (-73.78496 42.63993)
## 7   42.75459, -73.758714 POINT (-73.75871 42.75459)
## 8  42.601045, -73.790364 POINT (-73.79036 42.60104)
## 9  42.636568, -73.856367 POINT (-73.85637 42.63657)
## 10  42.698502, -73.89159  POINT (-73.89159 42.6985)
# set coordinate reference system to nys long island 

nysretailfood_sf_ <- st_transform(nysretailfood_sf, 2263)

st_crs(nysretailfood_sf_)
## Coordinate Reference System:
##   User input: EPSG:2263 
##   wkt:
## PROJCRS["NAD83 / New York Long Island (ftUS)",
##     BASEGEOGCRS["NAD83",
##         DATUM["North American Datum 1983",
##             ELLIPSOID["GRS 1980",6378137,298.257222101,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4269]],
##     CONVERSION["SPCS83 New York Long Island zone (US survey foot)",
##         METHOD["Lambert Conic Conformal (2SP)",
##             ID["EPSG",9802]],
##         PARAMETER["Latitude of false origin",40.1666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8821]],
##         PARAMETER["Longitude of false origin",-74,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8822]],
##         PARAMETER["Latitude of 1st standard parallel",41.0333333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8823]],
##         PARAMETER["Latitude of 2nd standard parallel",40.6666666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8824]],
##         PARAMETER["Easting at false origin",984250,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8826]],
##         PARAMETER["Northing at false origin",0,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8827]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##     USAGE[
##         SCOPE["Engineering survey, topographic mapping."],
##         AREA["United States (USA) - New York - counties of Bronx; Kings; Nassau; New York; Queens; Richmond; Suffolk."],
##         BBOX[40.47,-74.26,41.3,-71.8]],
##     ID["EPSG",2263]]
# check differences between coordinates

st_crs(nysretailfood_sf)$proj4string
## [1] "+proj=longlat +datum=WGS84 +no_defs"
st_crs(nysretailfood_sf_)$proj4string
## [1] "+proj=lcc +lat_0=40.1666666666667 +lon_0=-74 +lat_1=41.0333333333333 +lat_2=40.6666666666667 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs"
sf::st_bbox(nysretailfood_sf)
##      xmin      ymin      xmax      ymax 
## -79.75953  40.50782 -71.93873  44.99484
st_bbox(nysretailfood_sf_)
##      xmin      ymin      xmax      ymax 
## -574632.2  124371.4 1552535.4 1761473.6
# filter to nyc only using zip codes 

nysretailfood_sf_$Zip.Code <- as.character(nysretailfood_sf_$Zip.Code)
nyc_zip_sf$ZIPCODE <- as.character(nyc_zip_sf$ZIPCODE)

nysretailfood_nyc_sf <- nysretailfood_sf_ %>%
  filter(Zip.Code %in% nyc_zip_sf$ZIPCODE)

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

# mapview for all three sf objects 

mapview(nyc_zip_sf, layer.name='Zip Codes')
mapview(nyshealth_sf, layer.name='NYS Health Facilities')
mapview(nysretailfood_nyc_sf, layer.name='NYC Retail Food Stores')

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

# save all three sf objects into an RData file

save(nyc_zip_sf, nyshealth_sf, nysretailfood_nyc_sf,
     file = './data/nys_nyc_zipcodes_health_retailfood.RData')

# save all three sf objects into a single GeoPackage file/database

st_write(nyc_zip_sf, 
         dsn = './data/ny_zhr_data.gpkg', 
         layer='nyc_zip_codes',
         delete_layer = TRUE)
## Deleting layer `nyc_zip_codes' using driver `GPKG'
## Writing layer `nyc_zip_codes' to data source 
##   `./data/ny_zhr_data.gpkg' using driver `GPKG'
## Writing 263 features with 12 fields and geometry type Polygon.
st_write(nyshealth_sf,          
         dsn = './data/ny_zhr_data.gpkg', 
         layer='nys_health_facilities',
         delete_layer = TRUE)
## Deleting layer `nys_health_facilities' using driver `GPKG'
## Writing layer `nys_health_facilities' to data source 
##   `./data/ny_zhr_data.gpkg' using driver `GPKG'
## Writing 3848 features with 34 fields and geometry type Point.
st_write(nysretailfood_nyc_sf,          
         dsn = './data/ny_zhr_data.gpkg', 
         layer='nyc_retail_food_stores',
         delete_layer = TRUE)
## Deleting layer `nyc_retail_food_stores' using driver `GPKG'
## Writing layer `nyc_retail_food_stores' to data source 
##   `./data/ny_zhr_data.gpkg' using driver `GPKG'
## Writing 11372 features with 16 fields and geometry type Point.