Skip to contents

Example - finding hospital admission counts for mental health

# get the available datasets
d_datasets <- get_datasets()

# search the condition_name for Mental Health related string
d_datasets |>
  filter(str_detect(tolower(reported_measure_name), "mental")) |>
  glimpse()
#> Rows: 12
#> Columns: 8
#> $ data_id               <chr> "904", "1329", "1336", "2735", "2742", "3515", "…
#> $ data_name             <chr> "ADM 2011-12 - Number of admissions to hospital"…
#> $ start_date            <chr> "2011-07-01", "2012-07-01", "2013-07-01", "2014-…
#> $ end_date              <chr> "2012-06-30", "2013-06-30", "2014-06-30", "2015-…
#> $ outcome_measure_code  <chr> "MYH0024", "MYH0024", "MYH0024", "MYH0024", "MYH…
#> $ outcome_measure_name  <chr> "Number of admissions to hospital", "Number of a…
#> $ reported_measure_code <chr> "MYH-RM0216", "MYH-RM0216", "MYH-RM0216", "MYH-R…
#> $ reported_measure_name <chr> "Mental health", "Mental health", "Mental health…

# extract the dataset IDs
dataset_ids <- d_datasets |>
  filter(str_detect(tolower(reported_measure_name), "mental")) |>
  pull(data_id)

# read and combine the dataset_ids
d_mental_health_admissions <- read_dataset_ids(dataset_ids)

# aggregate by reporting period
d_mental_health_admissions |>
  filter(unit_type_name == "Hospital") |>
  summarize(
    mental_health_admission_n = sum(as.numeric(number_of_admissions_to_hospital), na.rm = TRUE),
    .by = c(start_date, end_date)
  )
#> # A tibble: 12 × 3
#>    start_date end_date   mental_health_admission_n
#>    <chr>      <chr>                          <dbl>
#>  1 2011-07-01 2012-06-30                    108542
#>  2 2012-07-01 2013-06-30                    112624
#>  3 2013-07-01 2014-06-30                    114119
#>  4 2014-07-01 2015-06-30                    119799
#>  5 2015-07-01 2016-06-30                    138689
#>  6 2016-07-01 2017-06-30                    145112
#>  7 2017-07-01 2018-06-30                    141012
#>  8 2018-07-01 2019-06-30                    144800
#>  9 2019-07-01 2020-06-30                    143179
#> 10 2020-07-01 2021-06-30                    142318
#> 11 2021-07-01 2022-06-30                    133535
#> 12 2022-07-01 2023-06-30                    135711