R Spatial Lab Assignment # 2

Task 1:

covid19 <- read_csv("C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_8/Data/tests-by-zcta_2021_04_23.csv", 
                       show_col_types = FALSE) %>%  
  clean_names()
nyc_zip <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_7/Data/nyc/ny_data.gpkg', 
         layer = 'nyc_zip') %>%  
  clean_names()
## Reading layer `nyc_zip' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_7\Data\nyc\ny_data.gpkg' 
##   using driver `GPKG'
## 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)
nyc_zip <- st_transform(nyc_zip, crs = 4326)

covid19_sf_merged <- base::merge(nyc_zip, covid19, by.x = "zipcode", 
                                 by.y = "modified_zcta")
mapview(covid19_sf_merged, zcol='total_covid_tests', layer.name='Total Covid Test')

Task 2:

nyc_food_sf <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_7/Data/nyc/ny_data.gpkg', 
         layer = 'nyc_retail')
## Reading layer `nyc_retail' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_7\Data\nyc\ny_data.gpkg' 
##   using driver `GPKG'
## 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
nyc_food_retail <- nyc_food_sf %>% 
  dplyr::filter(stringr::str_detect(establishment_type, '[AJD]')) %>% 
  sf::st_join(nyc_zip, ., join = st_contains) %>% 
  group_by(zipcode) %>% 
  summarize(food_location = n ())

mapview(nyc_food_retail, zcol='food_location', layer.name= 'NYC Food Stores')

Task 3:

nyc_health_sf <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_7/Data/nyc/ny_data.gpkg', 
         layer = 'nys_health') %>%  
  clean_names() %>% 
  filter(facility_county %in% c("Bronx", "Kings", "New York", "Queens", "Richmond"))
## Reading layer `nys_health' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_7\Data\nyc\ny_data.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 3843 features and 34 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -79.6299 ymin: 40.51677 xmax: -72.17 ymax: 44.97849
## Geodetic CRS:  WGS 84
nyc_nursing_home <- nyc_health_sf %>% 
  dplyr::filter(short_description == 'NH') %>% 
  sf::st_join(nyc_zip, ., join = st_contains) %>% 
  group_by(zipcode) %>% 
  summarize(NH_location = n ())

mapview(nyc_nursing_home, zcol='NH_location', layer.name = "NYC Nursing Homes")

Task 4:

nyc_census <- st_read("C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_8/Data/geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp")
## Reading layer `geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a' from data source `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_8\Data\geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2165 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25559 ymin: 40.49612 xmax: -73.70001 ymax: 40.91553
## Geodetic CRS:  WGS84(DD)
nyc_census %<>% dplyr::mutate(cntyFIPS = case_when(
  boro_name == 'Bronx' ~ '005',
  boro_name == 'Brooklyn' ~ '047',
  boro_name == 'Manhattan' ~ '061',
  boro_name == 'Queens' ~ '081',
  boro_name == 'Staten Island' ~ '085'),
  tractFIPS = paste(cntyFIPS, ct2010, sep=''))

acs_data <- readLines("C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_8/Data/ACSDP5Y2018.DP05_data_with_overlays_2020-04-22T132935.csv") %>% 
  magrittr::extract(-2) %>% 
  textConnection() %>%
  read.csv(header=TRUE, quote= "\"") %>%
  dplyr::select(GEO_ID, 
                totPop = DP05_0001E, elderlyPop = DP05_0024E, # >= 65
                malePop = DP05_0002E, femalePop = DP05_0003E,  
                whitePop = DP05_0037E, blackPop = DP05_0038E,
                asianPop = DP05_0067E, hispanicPop = DP05_0071E,
                adultPop = DP05_0021E, citizenAdult = DP05_0087E) %>%
  dplyr::mutate(censusCode = stringr::str_sub(GEO_ID, -9,-1));acs_data %>%
  magrittr::extract(1:10,)
##                  GEO_ID totPop elderlyPop malePop femalePop whitePop blackPop
## 1  1400000US36005000100   7080         51    6503       577     1773     4239
## 2  1400000US36005000200   4542        950    2264      2278     2165     1279
## 3  1400000US36005000400   5634        710    2807      2827     2623     1699
## 4  1400000US36005001600   5917        989    2365      3552     2406     2434
## 5  1400000US36005001900   2765         76    1363      1402      585     1041
## 6  1400000US36005002000   9409        977    4119      5290     3185     4487
## 7  1400000US36005002300   4600        648    2175      2425      479     2122
## 8  1400000US36005002400    172          0     121        51       69       89
## 9  1400000US36005002500   5887        548    2958      2929      903     1344
## 10 1400000US36005002701   2868        243    1259      1609      243      987
##    asianPop hispanicPop adultPop citizenAdult censusCode
## 1       130        2329     6909         6100  005000100
## 2       119        3367     3582         2952  005000200
## 3       226        3873     4507         4214  005000400
## 4        68        3603     4416         3851  005001600
## 5       130        1413     2008         1787  005001900
## 6        29        5905     6851         6170  005002000
## 7        27        2674     3498         3056  005002300
## 8        14           0      131           42  005002400
## 9        68        4562     4237         2722  005002500
## 10        0        1985     1848         1412  005002701
census_pop <- merge(nyc_census, acs_data, by.x ='tractFIPS', by.y = 'censusCode') %>% 
  st_transform(census_pop, crs = 4326)

str(census_pop)
## Classes 'sf' and 'data.frame':   2165 obs. of  25 variables:
##  $ tractFIPS   : chr  "005000100" "005000200" "005000400" "005001600" ...
##  $ boro_code   : chr  "2" "2" "2" "2" ...
##  $ boro_ct201  : chr  "2000100" "2000200" "2000400" "2001600" ...
##  $ boro_name   : chr  "Bronx" "Bronx" "Bronx" "Bronx" ...
##  $ cdeligibil  : chr  "I" "I" "I" "E" ...
##  $ ct2010      : chr  "000100" "000200" "000400" "001600" ...
##  $ ctlabel     : chr  "1" "2" "4" "16" ...
##  $ ntacode     : chr  "BX98" "BX09" "BX09" "BX09" ...
##  $ ntaname     : chr  "Rikers Island" "Soundview-Castle Hill-Clason Point-Harding Park" "Soundview-Castle Hill-Clason Point-Harding Park" "Soundview-Castle Hill-Clason Point-Harding Park" ...
##  $ puma        : chr  "3710" "3709" "3709" "3709" ...
##  $ shape_area  : num  18163828 5006558 8561175 5221330 17961359 ...
##  $ shape_leng  : num  18898 15611 24725 9671 30000 ...
##  $ cntyFIPS    : chr  "005" "005" "005" "005" ...
##  $ GEO_ID      : chr  "1400000US36005000100" "1400000US36005000200" "1400000US36005000400" "1400000US36005001600" ...
##  $ totPop      : int  7080 4542 5634 5917 2765 9409 4600 172 5887 2868 ...
##  $ elderlyPop  : int  51 950 710 989 76 977 648 0 548 243 ...
##  $ malePop     : int  6503 2264 2807 2365 1363 4119 2175 121 2958 1259 ...
##  $ femalePop   : int  577 2278 2827 3552 1402 5290 2425 51 2929 1609 ...
##  $ whitePop    : int  1773 2165 2623 2406 585 3185 479 69 903 243 ...
##  $ blackPop    : int  4239 1279 1699 2434 1041 4487 2122 89 1344 987 ...
##  $ asianPop    : int  130 119 226 68 130 29 27 14 68 0 ...
##  $ hispanicPop : int  2329 3367 3873 3603 1413 5905 2674 0 4562 1985 ...
##  $ adultPop    : int  6909 3582 4507 4416 2008 6851 3498 131 4237 1848 ...
##  $ citizenAdult: int  6100 2952 4214 3851 1787 6170 3056 42 2722 1412 ...
##  $ geometry    :sfc_MULTIPOLYGON of length 2165; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:499, 1:2] -73.9 -73.9 -73.9 -73.9 -73.9 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "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:24] "tractFIPS" "boro_code" "boro_ct201" "boro_name" ...

Task 5:

census_zip <- st_transform(census_pop, crs = 4326)

census_zip <- sf::st_join(nyc_zip, census_pop %>% sf::st_centroid(), 
                          join = st_contains) %>% 
  group_by(zipcode) %>% 
  summarize(totPop = sum(totPop),
            malePop = sum(malePop),
            femalePop = sum(femalePop),
            whitePop = sum(whitePop),
            asianPop = sum(asianPop),
            blackPop = sum(blackPop),
            hispanicPop = sum(hispanicPop),
            adultPop = sum(adultPop),
            citizenAdult = sum(citizenAdult))
## Warning: st_centroid assumes attributes are constant over geometries
mapview(census_zip, zcol='totPop', layer.name='Total Population')