title: “R Week 7 Assignment” author: “Genesis Santos date:”3/15/26” output: html_document
Don’t use a single chunk for the entire assignment. Break it into multiple chunks.
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.
# You R code comes here
nys_health <- read_csv("NYS_Health_Facility.csv", show_col_types = FALSE)
nys_health <- nys_health %>%
clean_names()
glimpse(nys_health)
## Rows: 3,990
## Columns: 36
## $ facility_id <dbl> 204, 620, 654, 1156, 2589, 3455, 3853, 42…
## $ facility_name <chr> "Hospice at Lourdes", "Charles T Sitrin H…
## $ short_description <chr> "HSPC", "NH", "NH", "NH", "NH", "NH", "DT…
## $ description <chr> "Hospice", "Residential Health Care Facil…
## $ facility_open_date <chr> "06/01/1985", "02/01/1989", "02/01/1989",…
## $ facility_address_1 <chr> "4102 Old Vestal Road", "2050 Tilden Aven…
## $ facility_address_2 <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ facility_city <chr> "Vestal", "New Hartford", "Syracuse", "Wa…
## $ facility_state <chr> "New York", "New York", "New York", "New …
## $ facility_zip_code <chr> "13850", "13413", "13205", "14569", "1489…
## $ facility_phone_number <dbl> 6077985692, 3157973114, 3154751641, 58578…
## $ facility_fax_number <dbl> NA, NA, NA, NA, NA, NA, NA, NA, 631465653…
## $ facility_website <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ facility_county_code <dbl> 3, 32, 33, 60, 2, 14, 29, 14, 29, 7093, 2…
## $ facility_county <chr> "Broome", "Oneida", "Onondaga", "Wyoming"…
## $ regional_office_id <dbl> 3, 3, 3, 1, 1, 1, 7, 1, 7, 5, 7, 3, 5, 3,…
## $ regional_office <chr> "Central New York Regional Office", "Cent…
## $ main_site_name <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, "NYU …
## $ main_site_facility_id <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, 1463,…
## $ operating_certificate_number <chr> "0301501F", "3227304N", "3301326N", "6027…
## $ operator_name <chr> "Our Lady of Lourdes Memorial Hospital In…
## $ operator_address_1 <chr> "169 Riverside Drive", "Box 1000 Tilden A…
## $ operator_address_2 <chr> NA, NA, NA, NA, NA, NA, NA, NA, "Suite 11…
## $ operator_city <chr> "Binghamton", "New Hartford", "Syracuse",…
## $ operator_state <chr> "New York", "New York", "New York", "New …
## $ operator_zip_code <chr> "13905", "13413", "13205", "14569", "1489…
## $ cooperator_name <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ cooperator_address <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ cooperator_address_2 <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ cooperator_city <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ cooperator_state <chr> "New York", "New York", "New York", "New …
## $ cooperator_zip_code <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ ownership_type <chr> "Not for Profit Corporation", "Not for Pr…
## $ facility_latitude <dbl> 42.09710, 43.05497, NA, 42.73898, 42.1264…
## $ facility_longitude <dbl> -75.97524, -75.22883, NA, -78.12867, -77.…
## $ facility_location <chr> "(42.097095, -75.975243)", "(43.05497, -7…
summary(nys_health[, c("facility_latitude", "facility_longitude")])
## facility_latitude facility_longitude
## Min. :-75.46 Min. :-79.63
## 1st Qu.: 40.77 1st Qu.:-75.91
## Median : 41.37 Median :-73.96
## Mean : 41.75 Mean :-74.77
## 3rd Qu.: 42.92 3rd Qu.:-73.84
## Max. : 44.98 Max. : 43.21
## NA's :142 NA's :142
nys_health_clean <- nys_health %>%
mutate(
facility_open_date = mdy(facility_open_date),
facility_latitude = as.numeric(facility_latitude),
facility_longitude = as.numeric(facility_longitude)
) %>%
filter(
!is.na(facility_latitude),
!is.na(facility_longitude),
facility_latitude >= -90, facility_latitude <= 90,
facility_longitude >= -180, facility_longitude <= 180
)
nys_health_sf <- st_as_sf(
nys_health_clean,
coords = c("facility_longitude", "facility_latitude"),
crs = 4326, # WGS 84 geographic coordinates
remove = FALSE
)
plot(st_geometry(nys_health_sf), main = "NYS Health Facilities")
The default chunk option will show the code as well as the output from the code.
Using, eval, echo, and
include, we can control whether run the code, show the
code, and include the results. See more details about chunk options at
https://rmarkdown.rstudio.com/lesson-3.html and https://yihui.org/knitr/options/.
The default option will run the code and show both code and results. In most cases, the default option is good for the assignments.
The following is a Quarto markdown code chunk (yes, we can also use Quarto in a RMarkdown document). Quarto markdown is different from R markdown in terms of chunk options.
Modern R Markdown (and Quarto) allows for a more organized way to
define chunk options using the “hash-pipe” (#|) syntax.
This keeps the logic of the code separate from the configuration of the
document. See chunk
options at Quarto website.
# your R code comes here.
food_stores <- read_csv("NYS_Retail_Food_Stores.csv", show_col_types = FALSE)
food_stores <- food_stores %>%
clean_names()
glimpse(food_stores)
## Rows: 29,389
## Columns: 15
## $ county <chr> "Albany", "Albany", "Albany", "Albany", "Albany", "…
## $ license_number <chr> "733149", "704590", "727909", "720557", "015890", "…
## $ operation_type <chr> "Store", "Store", "Store", "Store", "Store", "Store…
## $ establishment_type <chr> "A", "JAC", "JAC", "JAC", "A", "JAC", "JAC", "JAC",…
## $ entity_name <chr> "SPEEDWAY LLC", "1250 SELKIRK INC", "RED-KAP SALES …
## $ dba_name <chr> "12110", "1250 SELKIRK", "1667 GENERAL STORE", "19 …
## $ street_number <chr> "719", "1250", "1667", "315", "8", "477", "873", "1…
## $ street_name <chr> "NEW LOUDON RD", "RTE 9W & 396", "WESTERN AVENUE", …
## $ address_line_2 <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ address_line_3 <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ city <chr> "LATHAM", "SELKIRK", "ALBANY", "WATERVLIET", "ALBAN…
## $ state <chr> "NY", "NY", "NY", "NY", "NY", "NY", "NY", "NY", "NY…
## $ zip_code <dbl> 12110, 12158, 12203, 12189, 12210, 12209, 12110, 12…
## $ square_footage <dbl> 300, 3000, 2000, 1200, 1800, 0, 0, 200, 0, 2000, 40…
## $ location <chr> "719 NEW LOUDON RD\nLATHAM, NY 12110\n(42.739618, -…
head(food_stores$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)"
food_stores_clean <- food_stores %>%
mutate(
county = str_trim(county),
entity_name = str_squish(entity_name),
dba_name = str_squish(dba_name),
city = str_squish(city),
street_name = str_squish(street_name),
square_footage = as.numeric(str_remove_all(square_footage, ",")),
latitude = str_match(location, "\\(([-0-9.]+),\\s*([-0-9.]+)\\)")[,2],
longitude = str_match(location, "\\(([-0-9.]+),\\s*([-0-9.]+)\\)")[,3],
latitude = as.numeric(latitude),
longitude = as.numeric(longitude)
) %>%
filter(
county %in% c("Bronx", "Kings", "New York", "Queens", "Richmond"),
!is.na(latitude),
!is.na(longitude)
)
nyc_food_sf <- st_as_sf(
food_stores_clean,
coords = c("longitude", "latitude"),
crs = 4326,
remove = FALSE
)
print(nyc_food_sf)
## Simple feature collection with 11301 features and 17 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,301 × 18
## county license_number operation_type establishment_type entity_name dba_name
## * <chr> <chr> <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,291 more rows
## # ℹ 12 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>, latitude <dbl>,
## # longitude <dbl>, geometry <POINT [°]>
st_crs(nyc_food_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]]
head(nyc_food_sf)
## Simple feature collection with 6 features and 17 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -73.91961 ymin: 40.8251 xmax: -73.83188 ymax: 40.86916
## Geodetic CRS: WGS 84
## # A tibble: 6 × 18
## county license_number operation_type establishment_type entity_name dba_name
## <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Bronx 734149 Store JAC 7 ELEVEN FOO… <NA>
## 2 Bronx 606221 Store JAC 1001 SAN MIG… 1001 SA…
## 3 Bronx 606228 Store JAC 1029 FOOD PL… 1029 FO…
## 4 Bronx 723375 Store JAC 1078 DELI GR… 1078 DE…
## 5 Bronx 724807 Store JAC 1086 LUNA DE… 1086 LU…
## 6 Bronx 712943 Store JAC 109 AJ DELI … 109 AJ …
## # ℹ 12 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>, latitude <dbl>,
## # longitude <dbl>, geometry <POINT [°]>
plot(st_geometry(nyc_food_sf), main = "NYC Retail Food Stores")
Use this section to begin your spatial analysis.
# Example: Create and map a simple spatial point
st_point(c(-73.96, 40.77)) %>%
st_sfc(crs = 4326) %>%
mapview()
nys_health_sf <- st_transform(nys_health_sf, 4326)
nyc_food_sf <- st_transform(nyc_food_sf, 4326)
mapview(nys_health_sf, layer.name = "NYS Health Facilities")