R Spatial Lab Assignment # 1

task 1:

setwd("C:/Users/ogalagedara/Documents/week 8 p2")
coviddata <- read.csv('tests-by-zcta_2021_04_23.csv')
covidshapefile <- st_as_sf(coviddata,coords = c('lon','lat'), crs=4326)
nyc_zips <- st_read("C:/Users/ogalagedara/Documents/week 8 p2/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `C:\Users\ogalagedara\Documents\week 8 p2\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)
zipcodes_sf <- st_transform(nyc_zips,4326)
zipcodesmerged <- st_join(zipcodes_sf,covidshapefile)
plot(zipcodesmerged['COVID_CASE_COUNT'])

task 2:

setwd("C:/Users/ogalagedara/Documents/week 8 p2")
nyc_foods <- st_read('nycFoodStore.shp')
## Reading layer `nycFoodStore' from data source 
##   `C:\Users\ogalagedara\Documents\week 8 p2\nycFoodStore.shp' 
##   using driver `ESRI Shapefile'
## 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
nycfoodsshp <- st_transform(nyc_foods,4326)
zipcodesfinal <-zipcodesmerged %>% st_join(nycfoodsshp)%>%group_by(geometry)%>%mutate(n_food_stores=n())
plot(zipcodesfinal["n_food_stores"])

task 3

setwd("C:/Users/ogalagedara/Documents/week 8 p2")
nychealth<-read_csv("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.
missing_long <- is.na(nychealth$`Facility Longitude`)
missing_lat <- is.na(nychealth$`Facility Latitude`)
missing_coords <- missing_long | missing_lat
nychealth_clean <- nychealth[!missing_coords, ]
nychealthsf <- st_as_sf(nychealth_clean, coords = c("Facility Longitude", "Facility Latitude"), crs = 4326)
zipcode_health<-zipcodesmerged %>%
  st_join(nychealthsf) %>%
  group_by(geometry)%>%
  mutate(n_health_fac=n())
zipcode_health<-zipcode_health %>%
  filter(Description == "Hospital") %>%
  group_by(geometry)%>%
  mutate(n_hospital_fac=n())
zipcode_health <- zipcode_health %>%
  rename("Number of Hospitals" = "n_hospital_fac")
plot(zipcode_health['Number of Hospitals'])

Task 4

setwd("C:/Users/ogalagedara/Documents/week 8 p2")
census_trts <- st_read('geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a.shp')
## Reading layer `geo_export_1dc7b645-647b-4806-b9a0-7b79660f120a' from data source `C:\Users\ogalagedara\Documents\week 8 p2\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)
census_data_acs <- read.csv('ACSDP5Y2018.DP05_data_with_overlays_2020-04-22T132935.csv')
require(dplyr);
ct <- census_trts %<>% dplyr::mutate(cntyFIPS = case_when(
  boro_name == 'Bronx' ~ '005',
  boro_name == 'Brooklyn' ~ '047',
  boro_name == 'Queens' ~ '081',
  boro_name == 'Manhattan' ~ '061',
  boro_name == 'Staten Island' ~ '085'), tractsFIPS = paste(cntyFIPS, ct2010, sep='')
  )
ACS_cleaned <- census_data_acs %<>% 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) %>%
  mutate(censusCode = str_sub(GEO_ID,-9,-1))

ACS_cleaned %>% magrittr::extract(1:10,)
##                  GEO_ID                                  totPop
## 1                    id Estimate!!SEX AND AGE!!Total population
## 2  1400000US36005000100                                    7080
## 3  1400000US36005000200                                    4542
## 4  1400000US36005000400                                    5634
## 5  1400000US36005001600                                    5917
## 6  1400000US36005001900                                    2765
## 7  1400000US36005002000                                    9409
## 8  1400000US36005002300                                    4600
## 9  1400000US36005002400                                     172
## 10 1400000US36005002500                                    5887
##                                                    elderlyPop
## 1  Estimate!!SEX AND AGE!!Total population!!65 years and over
## 2                                                          51
## 3                                                         950
## 4                                                         710
## 5                                                         989
## 6                                                          76
## 7                                                         977
## 8                                                         648
## 9                                                           0
## 10                                                        548
##                                          malePop
## 1  Estimate!!SEX AND AGE!!Total population!!Male
## 2                                           6503
## 3                                           2264
## 4                                           2807
## 5                                           2365
## 6                                           1363
## 7                                           4119
## 8                                           2175
## 9                                            121
## 10                                          2958
##                                          femalePop
## 1  Estimate!!SEX AND AGE!!Total population!!Female
## 2                                              577
## 3                                             2278
## 4                                             2827
## 5                                             3552
## 6                                             1402
## 7                                             5290
## 8                                             2425
## 9                                               51
## 10                                            2929
##                                             whitePop
## 1  Estimate!!RACE!!Total population!!One race!!White
## 2                                               1773
## 3                                               2165
## 4                                               2623
## 5                                               2406
## 6                                                585
## 7                                               3185
## 8                                                479
## 9                                                 69
## 10                                               903
##                                                                 blackPop
## 1  Estimate!!RACE!!Total population!!One race!!Black or African American
## 2                                                                   4239
## 3                                                                   1279
## 4                                                                   1699
## 5                                                                   2434
## 6                                                                   1041
## 7                                                                   4487
## 8                                                                   2122
## 9                                                                     89
## 10                                                                  1344
##                                                                                        asianPop
## 1  Estimate!!Race alone or in combination with one or more other races!!Total population!!Asian
## 2                                                                                           130
## 3                                                                                           119
## 4                                                                                           226
## 5                                                                                            68
## 6                                                                                           130
## 7                                                                                            29
## 8                                                                                            27
## 9                                                                                            14
## 10                                                                                           68
##                                                                                  hispanicPop
## 1  Estimate!!HISPANIC OR LATINO AND RACE!!Total population!!Hispanic or Latino (of any race)
## 2                                                                                       2329
## 3                                                                                       3367
## 4                                                                                       3873
## 5                                                                                       3603
## 6                                                                                       1413
## 7                                                                                       5905
## 8                                                                                       2674
## 9                                                                                          0
## 10                                                                                      4562
##                                                      adultPop
## 1  Estimate!!SEX AND AGE!!Total population!!18 years and over
## 2                                                        6909
## 3                                                        3582
## 4                                                        4507
## 5                                                        4416
## 6                                                        2008
## 7                                                        6851
## 8                                                        3498
## 9                                                         131
## 10                                                       4237
##                                                                 citizenAdult
## 1  Estimate!!CITIZEN, VOTING AGE POPULATION!!Citizen, 18 and over population
## 2                                                                       6100
## 3                                                                       2952
## 4                                                                       4214
## 5                                                                       3851
## 6                                                                       1787
## 7                                                                       6170
## 8                                                                       3056
## 9                                                                         42
## 10                                                                      2722
##    censusCode
## 1          id
## 2   005000100
## 3   005000200
## 4   005000400
## 5   005001600
## 6   005001900
## 7   005002000
## 8   005002300
## 9   005002400
## 10  005002500
populationplot <- merge(ct,ACS_cleaned,by.x = 'tractsFIPS', by.y = 'censusCode')
plot(populationplot['blackPop'])

Task 5

setwd("C:/Users/ogalagedara/Documents/week 8 p2")
totalpop <- st_transform(populationplot, 4326)
ACS_cleaned <- zipcodes_sf %>% st_join(totalpop) %>% group_by(ZIPCODE)
plot(ACS_cleaned['totPop'])