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:

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

cat(smpCode)
## hello, R markdown and RPubs!

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.

postal_areas <- st_read("../R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `/Users/areeba/Desktop/R/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)
str(postal_areas)
## 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.

nys_health_facilities_df <- read_csv("../R-Spatial_I_Lab/NYS_Health_Facility.csv")
## Rows: 3990 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (28): Facility Name, Short Description, Description, Facility Open Date,...
## dbl  (8): Facility ID, Facility Phone Number, Facility Fax Number, Facility ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
str(nys_health_facilities_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>
nys_health_facilities_df_clean <- nys_health_facilities_df %>% filter(!is.na(`Facility Longitude`) & !is.na(`Facility Latitude`))

nys_health_facilities_sf <- st_as_sf(nys_health_facilities_df_clean, 
                               coords = c("Facility Longitude", "Facility Latitude"), crs = 4326)
nys_health_facilities_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.

nys_retail_food_df <- read_csv(
  "../R-Spatial_I_Lab/nys_retail_food_store_xy.csv",
  show_col_types = FALSE
)
names(nys_retail_food_df)[1] <- "County"
names(nys_retail_food_df)
##  [1] "County"             "License.Number"     "Operation.Type"    
##  [4] "Establishment.Type" "Entity.Name"        "DBA.Name"          
##  [7] "Street.Number"      "Street.Name"        "Address.Line.2"    
## [10] "Address.Line.3"     "City"               "State"             
## [13] "Zip.Code"           "Square.Footage"     "Location"          
## [16] "Coords"             "Y"                  "X"
unique(nys_retail_food_df$County)
##  [1] "Albany"       "Allegany"     "Bronx"        "Broome"       "Cattaraugus" 
##  [6] "Cayuga"       "Chautauqua"   "Chemung"      "Chenango"     "Clinton"     
## [11] "Columbia"     "Cortland"     "Delaware"     "Dutchess"     "Erie"        
## [16] "Essex"        "Franklin"     "Fulton"       "Genesee"      "Greene"      
## [21] "Hamilton"     "Herkimer"     "Jefferson"    "Kings"        "Lewis"       
## [26] "Livingston"   "Madison"      "Monroe"       "Montgomery"   "Nassau"      
## [31] "New York"     "Niagara"      "Oneida"       "Onondaga"     "Ontario"     
## [36] "Orange"       "Orleans"      "Oswego"       "Otsego"       "Putnam"      
## [41] "Queens"       "Rensselaer"   "Richmond"     "Rockland"     "Saratoga"    
## [46] "Schenectady"  "Schoharie"    "Schuyler"     "Seneca"       "Steuben"     
## [51] "St. Lawrence" "Suffolk"      "Sullivan"     "Tioga"        "Tompkins"    
## [56] "Ulster"       "Warren"       "Washington"   "Wayne"        "Westchester" 
## [61] "Wyoming"      "Yates"
nyc_retail_food_df <- nys_retail_food_df %>% filter(County %in% c("Bronx", "Kings", "New York", "Queens", "Richmond")) %>% filter(!is.na(X) & !is.na(Y))

nyc_retail_sf <- st_as_sf(nyc_retail_food_df, coords = c("X", "Y"), crs = 4326)

nyc_retail_sf
## Simple feature collection with 11300 features and 16 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.2484 ymin: 40.50782 xmax: -73.67061 ymax: 40.91008
## Geodetic CRS:  WGS 84
## # A tibble: 11,300 × 17
##    County License.Number Operation.Type Establishment.Type Entity.Name  DBA.Name
##  * <chr>           <dbl> <chr>          <chr>              <chr>        <chr>   
##  1 Bronx          734149 Store          JAC                7 ELEVEN FO… <NA>    
##  2 Bronx          606221 Store          JAC                1001 SAN MI… 1001 SA…
##  3 Bronx          606228 Store          JAC                1029 FOOD P… 1029 FO…
##  4 Bronx          723375 Store          JAC                1078 DELI G… 1078 DE…
##  5 Bronx          724807 Store          JAC                1086 LUNA D… 1086 LU…
##  6 Bronx          712943 Store          JAC                109 AJ DELI… 109 AJ …
##  7 Bronx          703060 Store          JAC                10 NEIGHBOR… 10 NEIG…
##  8 Bronx          609065 Store          JAC                1105 TINTON… 1105 TI…
##  9 Bronx          722972 Store          A                  1150 WEBSTE… 1150 WE…
## 10 Bronx          609621 Store          JAC                1158 GROCER… 1158 GR…
## # ℹ 11,290 more rows
## # ℹ 11 more variables: Street.Number <chr>, Street.Name <chr>,
## #   Address.Line.2 <lgl>, Address.Line.3 <lgl>, City <chr>, State <chr>,
## #   Zip.Code <dbl>, Square.Footage <dbl>, Location <chr>, Coords <chr>,
## #   geometry <POINT [°]>

task 5:

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

mapview(postal_areas)
mapview(nys_health_facilities_sf)
mapview(nyc_retail_sf)

task 6:

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

save(postal_areas, nys_health_facilities_sf,nyc_retail_sf, 
     file = '../R-Spatial_I_Lab/Lab1_sf_files.RData')