diff --git a/R/data.R b/R/data.R index 327711a..9015d6d 100644 --- a/R/data.R +++ b/R/data.R @@ -216,6 +216,22 @@ #' @source \url{https://www.ons.gov.uk/datasets/wellbeing-local-authority/editions/time-series/versions/4} "people_happiness" +#' Average Life Expectancy for Men and Women (2020-2022) +#' +#' A dataset containing statistics on average life expectancy for men +#' and women, by Northern Irish Council (2020-2022). +#' +#' @format A data frame with 11 rows and 3 variables: +#' \describe{ +#' \item{ltla24_code}{Local Authority Code} +#' \item{life_expectancy_combined}{Average life expectancy for men and women} +#' \item{year}{Time period - three year aggregate} +#' +#' ... +#' } +#' @source \url{https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022} +"people_life_expectancy" + #' Average Measurement of Life Worthwhileness Out of 10 (2022-23) #' #' A dataset containing statistics of personal ratings on feelings of diff --git a/data-raw/healthy-people/healthy-life-expectancy.R b/data-raw/healthy-people/healthy-life-expectancy.R index 874e3e3..5d9884a 100644 --- a/data-raw/healthy-people/healthy-life-expectancy.R +++ b/data-raw/healthy-people/healthy-life-expectancy.R @@ -1,124 +1,60 @@ -# ---- Load libraries ---- +# ---- Load packages ---- library(tidyverse) -library(httr) library(readxl) -library(geographr) -library(sf) -source("R/utils.R") +# ---- Get and clean data ---- +# Life Expectancy (2020-2022) +# Source: https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022 -# ---- Retrieve data ---- -GET( - "https://www.ons.gov.uk/file?uri=%2fpeoplepopulationandcommunity%2fhealthandsocialcare%2fhealthandlifeexpectancies%2fdatasets%2fhealthstatelifeexpectancyatbirthandatage65bylocalareasuk%2fcurrent/hsleatbirthandatage65byukla201618.xlsx", - write_disk(tf <- tempfile(fileext = ".xlsx")) -) +url <- "https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022/between2001to2003and2020to2022/lifeexpectancylocalareas.xlsx" +temp_file <- tempfile(fileext = ".xlsx") +download.file(url, temp_file, mode = "wb") -raw_males <- - read_excel( - tf, - sheet = "HE - Male at birth", - range = "A4:I490" - ) +le_raw <- read_excel(temp_file, sheet = 5, skip = 11) -hle_males <- - raw_males |> - select( - lad_code = `Area Codes`, - healthy_life_expectancy_male = HLE - ) |> - filter_codes(lad_code, "^N") -raw_females <- - read_excel( - tf, - sheet = "HE - Female at birth", - range = "A4:I490" - ) +# Life Expectancy (Male) -hle_females <- - raw_females |> - select( - lad_code = `Area Codes`, - healthy_life_expectancy_female = HLE +male_le <- le_raw |> + filter( + Sex == "Male", + str_starts(`Area code`, "N"), + `Age group` == "<1", + `Area code` != "N92000002" ) |> - filter_codes(lad_code, "^N") - -hle_joined <- - hle_males |> - left_join(hle_females) - -# ---- Calculate female/male weighted population estimates ---- -GET( - "https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/populationestimatesforukenglandandwalesscotlandandnorthernireland/mid2019april2020localauthoritydistrictcodes/ukmidyearestimates20192020ladcodes.xls", - write_disk(tf <- tempfile(fileext = ".xls")) -) - -# Total pop -raw_pop_total <- - read_excel( - tf, - sheet = "MYE2 - Persons", - range = "A5:D431" - ) - -pop_total <- - raw_pop_total |> select( - lad_code = Code, pop_total = `All ages` - ) |> - filter_codes(lad_code, "^N") - -# Males -raw_pop_male <- - read_excel( - tf, - sheet = "MYE2 - Males", - range = "A5:D431" + ltla24_code = `Area code`, + life_expectancy_male = `Life expectancy (years)...62` ) -pop_male <- - raw_pop_male |> - select( - lad_code = Code, pop_male = `All ages` - ) |> - filter_codes(lad_code, "^N") +# Life Expectancy (Female) -# Females -raw_pop_female <- - read_excel( - tf, - sheet = "MYE2 - Females", - range = "A5:D431" +female_le <- le_raw |> + filter( + Sex == "Female", + str_starts(`Area code`, "N"), + `Age group` == "<1", + `Area code` != "N92000002" + ) |> + select( + ltla24_code = `Area code`, + life_expectancy_female = `Life expectancy (years)...62` ) -pop_female <- - raw_pop_female |> - select( - lad_code = Code, pop_female = `All ages` - ) |> - filter_codes(lad_code, "^N") -# Calculate proportions -pop_proportions <- - pop_total |> - left_join(pop_male) |> - left_join(pop_female) |> +# Join datasets +people_life_expectancy <- male_le |> + left_join(female_le, by = "ltla24_code") |> rowwise() |> mutate( - proportion_male = pop_male / pop_total, - proportion_female = pop_female / pop_total + life_expectancy_combined = (life_expectancy_male + life_expectancy_female) / 2, + year = "2020-2022" ) |> - ungroup() |> - select(lad_code, starts_with("proportion")) - -# Compute population weighted mean HLE -hle <- - hle_joined |> - left_join(pop_proportions) |> - rowwise(lad_code) |> - summarise( - healthy_life_expectancy = (healthy_life_expectancy_male * proportion_male) + (healthy_life_expectancy_female * proportion_female), - .groups = "drop" + select( + ltla24_code, + life_expectancy_combined, + year ) -write_rds(hle, "data/vulnerability/health-inequalities/northern-ireland/healthy-people/healthy-life-expectancy.rds") \ No newline at end of file +# ---- Save output to data/ folder ---- +usethis::use_data(people_life_expectancy, overwrite = TRUE) diff --git a/data/people_life_expectancy.rda b/data/people_life_expectancy.rda new file mode 100644 index 0000000..87b98bf Binary files /dev/null and b/data/people_life_expectancy.rda differ diff --git a/man/people_life_expectancy.Rd b/man/people_life_expectancy.Rd new file mode 100644 index 0000000..c07a885 --- /dev/null +++ b/man/people_life_expectancy.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{people_life_expectancy} +\alias{people_life_expectancy} +\title{Average Life Expectancy for Men and Women (2020-2022)} +\format{ +A data frame with 11 rows and 3 variables: +\describe{ +\item{ltla24_code}{Local Authority Code} +\item{life_expectancy_combined}{Average life expectancy for men and women} +\item{year}{Time period - three year aggregate} + +... +} +} +\source{ +\url{https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022} +} +\usage{ +people_life_expectancy +} +\description{ +A dataset containing statistics on average life expectancy for men +and women, by Northern Irish Council (2020-2022). +} +\keyword{datasets} diff --git a/metadata.md b/metadata.md index 9641652..9e328fe 100644 --- a/metadata.md +++ b/metadata.md @@ -36,7 +36,7 @@ | Healthy People | Suicides | 2019 | [NISRA](https://www.ninis2.nisra.gov.uk) | [OGL3](https://www.nisra.gov.uk/crown-copyright) | :heavy_exclamation_mark: Data unavailable - contact info@nisra.gov.uk and ask for Deaths by Cause (administrative geographies) | :x: | | Healthy People | Avoidable Deaths | 2015-19 | [NISRA](https://www.ninis2.nisra.gov.uk) | [OGL3](https://www.nisra.gov.uk/crown-copyright) | :heavy_exclamation_mark: Data unavailable - contact healthinequalities@health-ni.gov.uk and ask for Standardised Death Rate - Avoidable (administrative geographies) | :x: | | Healthy People | Mortality from All Causes | 2020-22 | [NISRA](https://data.nisra.gov.uk/) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Indicator: Age/sex-standardised all mortality rates per 100k | :heavy_check_mark: | -| Healthy People | Healthy Life Expectancy | 2016-2018 | [ONS](https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/healthstatelifeexpectancyatbirthandatage65bylocalareasuk) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: | :heavy_check_mark: | +| Healthy People | Life Expectancy | 2020-2022 | [ONS](https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Average healthy life expectancy for men and women combined. | :heavy_check_mark: | | Healthy People | Happiness | 2022-23 | [ONS](https://www.ons.gov.uk/datasets/wellbeing-local-authority/editions/time-series/versions/4) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Indicator: Average personal ratings on feelings of happiness out of 10.| :heavy_check_mark: | | Healthy People | Anxiety | 2022-23 | [ONS](https://www.ons.gov.uk/datasets/wellbeing-local-authority/editions/time-series/versions/4) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Indicator: Average personal ratings on feelings of anxiety out of 10. | :heavy_check_mark: | | Healthy People | Life Satisfaction | 2022-23 | [ONS](https://www.ons.gov.uk/datasets/wellbeing-local-authority/editions/time-series/versions/4) | [OGL3](https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/) | :heavy_check_mark: Indicator: Average personal ratings on feelings of life satisfaction out of 10.| :heavy_check_mark: |