csv-file “hospital_points” and the polygon of North-Rhine Westphalia. How many hospitals are located within the borders of NRW?
data folder and a shapefile of NRW. For NRW, you can either use Stefans OSM syntax or use the “german_states” shapefile and filter on AGS=="05".
The default of st_join will leave you with a ‘left-join’ and returns a data object with all hospitals and matching district information for those which are located within NRW. You can reset the option to perform an ‘inner-join’ and keep only the observation which lay within the predefined area (st_join(x , y, join = "", left = FALSE)).
Did the operationalization of health care provision convince you? Don’t you think it might be more important how many hospitals are close to the respondents? To test this, we want to calculate the number of hospitals per district in North-Rhine Westphalia. Use the syntax below to prep the hospital data.
Earn extra points by counting not only the number of hospitals but also the sum of hospital beds within a district.
nrw_districts <- sf::st_read(dsn = "../data",
layer = "GER_DISTRICTS",
# quiet is optional if you
# not want to print the metainformation
quiet = T) %>%
sf::st_transform(. , 3035) %>%
dplyr::rename(., district_id = id) %>%
# filter the districts of NRW
dplyr::filter( district_id >= 5000 & district_id < 6000 )
nrw_hospitals <-
nrw_hospitals %>%
# beds were character, now numeric
dplyr::mutate(beds = as.numeric(beds)) %>%
# replace NAs as zeros for simplification
replace(., is.na(.), 0)
You need a as_tibble() data frame to use the functions group_by() and summarise().
The function n() allows summarising the total count of hospitals. sum(beds) for summarizing the bed total per district.