Explanation of the template

Update the title with your information. For example, it could be “GTECH_78520_2023S_Week12_JohnDoe25” for John Doe, 2022 Spring semester, week 12.

Also update the author name and date accordingly.

R Spaital 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:

nycpostalareas_sf <- st_read("./ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `/Users/mariyatrofymova/Downloads/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(nycpostalareas_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" ...
healthfacilities <- read.csv("./R-Spatial_I_Lab/NYS_Health_Facility.csv", stringsAsFactors = FALSE)
healthfacilities %>% 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)
healthfacilities_clean <- healthfacilities %>%
  filter(!is.na(Facility.Longitude) & !is.na(Facility.Latitude))

healthfacilities_sf <- st_as_sf(healthfacilities_clean, coords = c("Facility.Longitude", "Facility.Latitude"), crs = 4326, remove = FALSE)

head(healthfacilities_sf)
## Simple feature collection with 6 features and 36 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -78.68856 ymin: 40.73376 xmax: -73.59129 ymax: 43.05497
## Geodetic CRS:  WGS 84
##   Facility.ID                           Facility.Name Short.Description
## 1         204                      Hospice at Lourdes              HSPC
## 2         620 Charles T Sitrin Health Care Center Inc                NH
## 3        1156                  East Side Nursing Home                NH
## 4        2589            Wellsville Manor Care Center                NH
## 5        3455       Harris Hill Nursing Facility, LLC                NH
## 6        3853                Garden City Surgi Center               DTC
##                              Description Facility.Open.Date
## 1                                Hospice         06/01/1985
## 2 Residential Health Care Facility - SNF         02/01/1989
## 3 Residential Health Care Facility - SNF         08/01/1979
## 4 Residential Health Care Facility - SNF         02/01/1989
## 5 Residential Health Care Facility - SNF         04/08/1992
## 6        Diagnostic and Treatment Center         04/07/2008
##     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       62 Prospect St                           Warsaw       New York
## 4   4192A Bolivar Road                       Wellsville       New York
## 5    2699 Wehrle Drive                    Williamsville       New York
## 6        400 Endo Blvd                      Garden City       New York
##   Facility.Zip.Code Facility.Phone.Number Facility.Fax.Number Facility.Website
## 1             13850            6077985692                  NA                 
## 2             13413            3157973114                  NA                 
## 3             14569            5857868151                  NA                 
## 4             14895            5855934400                  NA                 
## 5             14221            7166323700                  NA                 
## 6             11530            5168328504                  NA                 
##   Facility.County.Code Facility.County Regional.Office.ID
## 1                    3          Broome                  3
## 2                   32          Oneida                  3
## 3                   60         Wyoming                  1
## 4                    2        Allegany                  1
## 5                   14            Erie                  1
## 6                   29          Nassau                  7
##                                   Regional.Office Main.Site.Name
## 1                Central New York Regional Office               
## 2                Central New York Regional Office               
## 3               Western Regional Office - Buffalo               
## 4               Western Regional Office - Buffalo               
## 5               Western Regional Office - Buffalo               
## 6 Metropolitan Area Regional Office - Long Island               
##   Main.Site.Facility.ID Operating.Certificate.Number
## 1                    NA                     0301501F
## 2                    NA                     3227304N
## 3                    NA                     6027303N
## 4                    NA                     0228305N
## 5                    NA                     1406301N
## 6                    NA                     2905204R
##                               Operator.Name             Operator.Address.1
## 1 Our Lady of Lourdes Memorial Hospital Inc            169 Riverside Drive
## 2  Charles T Sitrin Health Care Center, Inc         Box 1000 Tilden Avenue
## 3                East Side Nursing Home Inc             62 Prospect Street
## 4                      Wellsville Manor LLC             4192a Bolivar Road
## 5         Harris Hill Nursing Facility, LLC 560 Delaware Avenue, Suite 400
## 6                           Endo Group, LLC             400 Endo Boulevard
##   Operator.Address.2 Operator.City Operator.State Operator.Zip.Code
## 1                       Binghamton       New York             13905
## 2                     New Hartford       New York             13413
## 3                           Warsaw       New York             14569
## 4                       Wellsville       New York             14897
## 5                          Buffalo       New York             14202
## 6                      Garden City       New York             11530
##   Cooperator.Name Cooperator.Address Cooperator.Address.2 Cooperator.City
## 1                                                                        
## 2                                                                        
## 3                                                                        
## 4                                                                        
## 5                                                                        
## 6                                                                        
##   Cooperator.State Cooperator.Zip.Code             Ownership.Type
## 1         New York                  NA Not for Profit Corporation
## 2         New York                  NA Not for Profit Corporation
## 3         New York                  NA       Business Corporation
## 4         New York                  NA                        LLC
## 5         New York                  NA                        LLC
## 6         New York                  NA                        LLC
##   Facility.Latitude Facility.Longitude       Facility.Location
## 1          42.09710          -75.97524 (42.097095, -75.975243)
## 2          43.05497          -75.22883  (43.05497, -75.228828)
## 3          42.73898          -78.12867  (42.738979, -78.12867)
## 4          42.12646          -77.96783 (42.126461, -77.967834)
## 5          42.95627          -78.68856  (42.956268, -78.68856)
## 6          40.73376          -73.59129 (40.733765, -73.591286)
##                     geometry
## 1  POINT (-75.97524 42.0971)
## 2 POINT (-75.22883 43.05497)
## 3 POINT (-78.12867 42.73898)
## 4 POINT (-77.96783 42.12646)
## 5 POINT (-78.68856 42.95627)
## 6 POINT (-73.59129 40.73376)
foodstores <- read.csv("./R-Spatial_I_Lab/NYS_Retail_Food_Stores.csv", stringsAsFactors = FALSE)
foodstores %>% 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(foodstores$Location, "\\(")[,1]
rghtPos <- stringr::str_locate(foodstores$Location, "\\)")[,1]

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

coordinates <- stringr::str_locate(foodstores$Coords, ", ")

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

st_crs(foodstores_sf) <- 4326

nycpostalareas_sf <- st_transform(nycpostalareas_sf, st_crs(foodstores_sf))

foodstores_sf_with_zip <- st_join(foodstores_sf, nycpostalareas_sf, join = st_within)

foodstores_sf_nyc <- foodstores_sf_with_zip %>%
  filter(!is.na(ZIPCODE))

foodstores_sf_nyc <- select(foodstores_sf_nyc, -COUNTY, -STATE)

str(foodstores_sf_nyc)
## Classes 'sf' and 'data.frame':   11323 obs. of  27 variables:
##  $ County            : chr  "Bronx" "Bronx" "Bronx" "Bronx" ...
##  $ License.Number    : int  734149 606221 606228 723375 724807 712943 703060 609065 722972 609621 ...
##  $ Operation.Type    : chr  "Store" "Store" "Store" "Store" ...
##  $ Establishment.Type: chr  "JAC   " "JAC   " "JAC   " "JAC   " ...
##  $ Entity.Name       : chr  "7 ELEVEN FOOD STORE #37933H      " "1001 SAN MIGUEL FOOD CENTER INC  " "1029 FOOD PLAZA INC              " "1078 DELI GROCERY CORP           " ...
##  $ DBA.Name          : chr  "                       " "1001 SAN MIGUEL FD CNTR" "1029 FOOD PLAZA        " "1078 DELI GROCERY      " ...
##  $ Street.Number     : chr  "500" "1001" "122" "1078" ...
##  $ Street.Name       : chr  "BAYCHESTER AVE               " "SHERIDAN AVE                " "E 181ST ST                   " "EAST 165TH STREET           " ...
##  $ Address.Line.2    : logi  NA NA NA NA NA NA ...
##  $ Address.Line.3    : logi  NA NA NA NA NA NA ...
##  $ City              : chr  "BRONX             " "BRONX             " "BRONX             " "BRONX             " ...
##  $ State             : chr  "NY" "NY" "NY" "NY" ...
##  $ Zip.Code          : int  10475 10456 10453 10459 10456 10453 10467 10456 10456 10472 ...
##  $ Square.Footage    : chr  "0" "1,100" "2,000" "1,200" ...
##  $ Location          : chr  "500 BAYCHESTER AVE\nBRONX, NY 10475\n(40.869156, -73.831875)" "1001 SHERIDAN AVE\nBRONX, NY 10456\n(40.829061, -73.919613)" "122 E 181ST ST\nBRONX, NY 10453\n(40.854755, -73.902853)" "1078 EAST 165TH STREET\nBRONX, NY 10459\n(40.825105, -73.890589)" ...
##  $ Coords            : chr  "40.869156, -73.831875" "40.829061, -73.919613" "40.854755, -73.902853" "40.825105, -73.890589" ...
##  $ ZIPCODE           : chr  "10475" "10456" "10453" "10459" ...
##  $ BLDGZIP           : chr  "0" "0" "0" "0" ...
##  $ PO_NAME           : chr  "Bronx" "Bronx" "Bronx" "Bronx" ...
##  $ POPULATION        : num  40931 89390 77576 46804 89390 ...
##  $ AREA              : num  38633297 29933452 25748509 22551941 29933452 ...
##  $ ST_FIPS           : chr  "36" "36" "36" "36" ...
##  $ CTY_FIPS          : chr  "005" "005" "005" "005" ...
##  $ 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_POINT of length 11323; first list element:  'XY' num  -73.8 40.9
##  - 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:26] "County" "License.Number" "Operation.Type" "Establishment.Type" ...
mapview(nycpostalareas_sf, 
        layer.name = "NYC Postal Areas", 
        homebutton = FALSE,
        legend = TRUE)
mapview(healthfacilities_sf, 
          layer.name = 'Health Facilities', 
          homebutton=FALSE, 
          legend=TRUE,
          color='lightblue',
          cex= 2)
mapview(foodstores_sf_nyc, 
          layer.name = 'Retail Food Stores', 
          homebutton=FALSE, 
          legend=TRUE,
          color='orange',
          cex= 2)
save(nycpostalareas_sf, 
     file = './R-Spatial_I_Lab/nycpostalareas_sf.RData')

save(healthfacilities_sf, 
     file = './R-Spatial_I_Lab/healthfacilities_sf.RData')

save(foodstores_sf_nyc, 
     file = './R-Spatial_I_Lab/foodstores_sf.RData')