From 13d46f8af38aff97d745a027f2d77717e248f610 Mon Sep 17 00:00:00 2001 From: CatReid Date: Tue, 17 Dec 2024 14:52:24 +0000 Subject: [PATCH 1/4] First draft started - HLE First draft started - HLE. Raw data uploaded; being split into male and female to then be combined --- .../healthy-people/healthy-life-expectancy.R | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/data-raw/healthy-people/healthy-life-expectancy.R b/data-raw/healthy-people/healthy-life-expectancy.R index 874e3e3..6e7e1ea 100644 --- a/data-raw/healthy-people/healthy-life-expectancy.R +++ b/data-raw/healthy-people/healthy-life-expectancy.R @@ -1,3 +1,39 @@ +# ---- Load packages ---- +library(tidyverse) +library(readxl) +library(geographr) + +# ---- Get and clean data ---- +# Healthy Life Expectancy +# Source: https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022 + +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") + +hle_raw <- read_excel(temp_file, sheet = 5, skip = 11) + + +# Healthy Life Expectancy (Male) + +male_hle <- hle_raw |> + filter(Sex == "Male", + str_starts(`Area code`, "N")) |> + select(ltla24_code = `Area code`) + + + + + + + +# Healthy Life Expectancy (Female) + +female_hle <- hle_raw |> + filter(Sex == "Female", + str_starts(`Area code`, "N")) + + # ---- Load libraries ---- library(tidyverse) library(httr) @@ -121,4 +157,4 @@ hle <- .groups = "drop" ) -write_rds(hle, "data/vulnerability/health-inequalities/northern-ireland/healthy-people/healthy-life-expectancy.rds") \ No newline at end of file +write_rds(hle, "data/vulnerability/health-inequalities/northern-ireland/healthy-people/healthy-life-expectancy.rds") From cf5b0d70dac3f3180d62f3ffc2fd7cd1c44ed4f9 Mon Sep 17 00:00:00 2001 From: CatReid Date: Tue, 17 Dec 2024 16:35:11 +0000 Subject: [PATCH 2/4] Life-expectancy for males and females calculated Life-expectancy for males and females calculated. Pausing before calculating population-weighted average; to discuss in Jan '25. --- .../healthy-people/healthy-life-expectancy.R | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/data-raw/healthy-people/healthy-life-expectancy.R b/data-raw/healthy-people/healthy-life-expectancy.R index 6e7e1ea..3e9ee35 100644 --- a/data-raw/healthy-people/healthy-life-expectancy.R +++ b/data-raw/healthy-people/healthy-life-expectancy.R @@ -4,34 +4,52 @@ library(readxl) library(geographr) # ---- Get and clean data ---- -# Healthy Life Expectancy +# Life Expectancy (2020-2022) # Source: https://www.ons.gov.uk/peoplepopulationandcommunity/healthandsocialcare/healthandlifeexpectancies/datasets/lifeexpectancyforlocalareasinenglandnorthernirelandandwalesbetween2001to2003and2020to2022 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") -hle_raw <- read_excel(temp_file, sheet = 5, skip = 11) +le_raw <- read_excel(temp_file, sheet = 5, skip = 11) -# Healthy Life Expectancy (Male) +# Life Expectancy (Male) -male_hle <- hle_raw |> +male_le <- le_raw |> filter(Sex == "Male", - str_starts(`Area code`, "N")) |> - select(ltla24_code = `Area code`) + str_starts(`Area code`, "N"), + `Age group` == "<1", + `Area code` != "N92000002") |> + select(ltla24_code = `Area code`, + life_expectancy_male = `Life expectancy (years)...62`) +# Life Expectancy (Female) +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`) +# Join datasets +people_life_expectancy <- male_le |> + left_join(female_le, by = "ltla24_code") -# Healthy Life Expectancy (Female) +# TO DO: add in population data (from NISRA: 'Population totals, MYE01T06' OR +# 'Mid-year population estimates, MYE01T04 - probably this one). +# +# Then need to get population totals for males and females. Then combine male +# and female population to get total population for each council. +# +# Then need to calculate the weighted life expectancy - +# ((male-LE * male-pop) + (female-LE * female-pop)) / total-pop -female_hle <- hle_raw |> - filter(Sex == "Female", - str_starts(`Area code`, "N")) # ---- Load libraries ---- From b7e0627d17a7ab1abc21c163de98cc8870268da9 Mon Sep 17 00:00:00 2001 From: CatReid Date: Tue, 14 Jan 2025 16:36:12 +0000 Subject: [PATCH 3/4] First draft completed - life-expectancy First draft completed - life-expectancy, styled and rendered. Average calculated between men and women. --- R/data.R | 16 ++ .../healthy-people/healthy-life-expectancy.R | 174 +++--------------- data/people_life_expectancy.rda | Bin 0 -> 457 bytes man/people_life_expectancy.Rd | 27 +++ metadata.md | 2 +- 5 files changed, 72 insertions(+), 147 deletions(-) create mode 100644 data/people_life_expectancy.rda create mode 100644 man/people_life_expectancy.Rd 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 3e9ee35..5d9884a 100644 --- a/data-raw/healthy-people/healthy-life-expectancy.R +++ b/data-raw/healthy-people/healthy-life-expectancy.R @@ -1,7 +1,6 @@ # ---- Load packages ---- library(tidyverse) library(readxl) -library(geographr) # ---- Get and clean data ---- # Life Expectancy (2020-2022) @@ -17,162 +16,45 @@ le_raw <- read_excel(temp_file, sheet = 5, skip = 11) # Life Expectancy (Male) male_le <- le_raw |> - filter(Sex == "Male", - str_starts(`Area code`, "N"), - `Age group` == "<1", - `Area code` != "N92000002") |> - select(ltla24_code = `Area code`, - life_expectancy_male = `Life expectancy (years)...62`) - -# Life Expectancy (Female) - -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`) - - -# Join datasets -people_life_expectancy <- male_le |> - left_join(female_le, by = "ltla24_code") - - - -# TO DO: add in population data (from NISRA: 'Population totals, MYE01T06' OR -# 'Mid-year population estimates, MYE01T04 - probably this one). -# -# Then need to get population totals for males and females. Then combine male -# and female population to get total population for each council. -# -# Then need to calculate the weighted life expectancy - -# ((male-LE * male-pop) + (female-LE * female-pop)) / total-pop - - - -# ---- Load libraries ---- -library(tidyverse) -library(httr) -library(readxl) -library(geographr) -library(sf) - -source("R/utils.R") - -# ---- Retrieve data ---- -GET( - "https://www.ons.gov.uk/file?uri=%2fpeoplepopulationandcommunity%2fhealthandsocialcare%2fhealthandlifeexpectancies%2fdatasets%2fhealthstatelifeexpectancyatbirthandatage65bylocalareasuk%2fcurrent/hsleatbirthandatage65byukla201618.xlsx", - write_disk(tf <- tempfile(fileext = ".xlsx")) -) - -raw_males <- - read_excel( - tf, - sheet = "HE - Male at birth", - range = "A4:I490" - ) - -hle_males <- - raw_males |> - select( - lad_code = `Area Codes`, - healthy_life_expectancy_male = HLE + filter( + Sex == "Male", + str_starts(`Area code`, "N"), + `Age group` == "<1", + `Area code` != "N92000002" ) |> - filter_codes(lad_code, "^N") - -raw_females <- - read_excel( - tf, - sheet = "HE - Female at birth", - range = "A4:I490" - ) - -hle_females <- - raw_females |> select( - lad_code = `Area Codes`, - healthy_life_expectancy_female = HLE - ) |> - 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" + ltla24_code = `Area code`, + life_expectancy_male = `Life expectancy (years)...62` ) -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" - ) +# Life Expectancy (Female) -pop_male <- - raw_pop_male |> - select( - lad_code = Code, pop_male = `All ages` +female_le <- le_raw |> + filter( + Sex == "Female", + str_starts(`Area code`, "N"), + `Age group` == "<1", + `Area code` != "N92000002" ) |> - filter_codes(lad_code, "^N") - -# Females -raw_pop_female <- - read_excel( - tf, - sheet = "MYE2 - Females", - range = "A5:D431" + 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") +# ---- 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 0000000000000000000000000000000000000000..87b98bf4d9100912dddf3045d9625e3e19aa7b0b GIT binary patch literal 457 zcmV;)0XF_ZT4*^jL0KkKS$Y?qH~<0Cf8YQ6O<@4z|Kmi#JVd{z-|#{JhyegV5C8yx zAP52humO>V(h{krifQQ7^vWc#Pl&YqT#Q4mDHR>49Mbs`ks z*k!kD-P+r|em$mz<>@vi0vIqYWkMl^7Lo*E3F-|<2w9+%05cEp`iKP&&`7gv zjN-bMk9}&kDyc${DUhUvEU7jX*)o!*bu^UZQc72%u{;E#WhJUbzF)lg`#Qf%9mI}! zrB92ki%LJCT&VE9WU?1(NoXK literal 0 HcmV?d00001 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: | From 30130fe298f1fa2715df7cb0bdca083573dca05b Mon Sep 17 00:00:00 2001 From: MikeJohnPage <38110953+MikeJohnPage@users.noreply.github.com> Date: Mon, 20 Jan 2025 18:35:55 +0100 Subject: [PATCH 4/4] Use idiomatic R way to calculate mean --- data-raw/healthy-people/healthy-life-expectancy.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-raw/healthy-people/healthy-life-expectancy.R b/data-raw/healthy-people/healthy-life-expectancy.R index 5d9884a..1d12816 100644 --- a/data-raw/healthy-people/healthy-life-expectancy.R +++ b/data-raw/healthy-people/healthy-life-expectancy.R @@ -47,7 +47,7 @@ people_life_expectancy <- male_le |> left_join(female_le, by = "ltla24_code") |> rowwise() |> mutate( - life_expectancy_combined = (life_expectancy_male + life_expectancy_female) / 2, + life_expectancy_combined = mean(c(life_expectancy_male, life_expectancy_female)), year = "2020-2022" ) |> select(