Skip to content

Instantly share code, notes, and snippets.

@tomjemmett
Created July 2, 2024 12:30
Show Gist options
  • Select an option

  • Save tomjemmett/9109470f696f854e54cb0777f4d6d18b to your computer and use it in GitHub Desktop.

Select an option

Save tomjemmett/9109470f696f854e54cb0777f4d6d18b to your computer and use it in GitHub Desktop.
library(tidyverse)
library(janitor)
get_hes_summary_stats_csv <- function(url) {
url |>
readr::read_csv() |>
clean_names() |>
select(-"uid") |>
filter(
org_level == "PROVIDER",
measure_type == "Summary",
measure %in% c("FCE_SUM", "FAE_SUM", "FAE_EMERGENCY_SUM", "FAE_OTHER_SUM", "FAE_PLANNED_SUM", "FAE_WAITLIST_SUM"),
str_detect(measure_value, "^\\d+$")
) |>
mutate(
across("measure_value", as.numeric)
) |>
pivot_wider(names_from = "measure", values_from = "measure_value") |>
transmute(
.data[["org_code"]],
.data[["org_description"]],
fae = .data[["FAE_SUM"]],
fce = .data[["FCE_SUM"]],
fae_emergency = .data[["FAE_EMERGENCY_SUM"]],
fae_elective = .data[["FAE_PLANNED_SUM"]] + .data[["FAE_WAITLIST_SUM"]],
fae_other = .data[["FAE_OTHER_SUM"]]
)
}
get_hes_summary_stats_xlsx <- function(url, sheet, skip) {
tf <- withr::local_tempfile()
download.file(url, tf, mode = "wb")
readxl::read_xlsx(
tf,
sheet = sheet,
skip = skip,
na = "*",
col_names = c(
"org_code",
"org_description",
"fae",
"fce",
"skip_1",
"skip_2",
"fae_emergency",
"fae_planned",
"fae_waiting_list",
"fae_other",
"skip_3",
"skip_4"
)
) |>
drop_na("org_description") |>
transmute(
org_code,
org_description,
fae,
fce,
fae_emergency,
fae_elective = fae_planned + fae_waiting_list,
fae_other
)
}
hes_summary_stats <- dplyr::bind_rows(
.id = "fyear",
"2019/20" = get_hes_summary_stats_xlsx(
"https://files.digital.nhs.uk/01/B9F273/hosp-epis-stat-admi-prov-leve-2019-20-tab%20v2.xlsx",
"Table A_1920",
26
),
"2020/21" = get_hes_summary_stats_xlsx(
"https://files.digital.nhs.uk/72/1ED61C/hosp-epis-stat-admi-prov-leve-2020-21-tab.xlsx",
"Table A_2021",
26
),
"2021/22" = get_hes_summary_stats_xlsx(
"https://files.digital.nhs.uk/85/7D3EFA/hosp-epis-stat-admi-pla-2021-22.xlsx",
"Table A_2021",
26
),
"2022/23" = get_hes_summary_stats_csv(
"https://files.digital.nhs.uk/D6/4B3B73/hosp-epis-stat-admi-pla-2022-23-data.csv"
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment