diff --git a/R/ons_uk_population_2023.R b/R/ons_uk_population_2023.R new file mode 100644 index 0000000..93f38e6 --- /dev/null +++ b/R/ons_uk_population_2023.R @@ -0,0 +1,48 @@ +#' ONS Mid-2023 Population Estimate for UK +#' +#' ONS Population Estimates for Mid-year 2023 +#' National and subnational mid-year population estimates for the UK and its +#' constituent countries by administrative area, age and sex (including +#' components of population change, median age and population density). +#' +#' ONS Estimates of the population for the UK, England, Wales, Scotland, and Northern Ireland +#' +#' @source +#' \url{https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/populationestimatesforukenglandandwalesscotlandandnorthernireland} +#' +#' @docType data +#' +#' @keywords datasets ons population +#' +#' @format Tibble with six columns +#' \describe{ +#' \item{sex}{male or female} +#' \item{Code}{country/geography code} +#' \item{Name}{country of the UK} +#' \item{Geography}{Country} +#' \item{age}{year of age} +#' \item{count}{the number of people in this group} +#' } +#' +#' @usage data(ons_uk_population_2023) +#' +#' @examples +#' data(ons_uk_population_2023) +#' +#' +#' library(dplyr) +#' library(tidyr) +#' +#' # create a dataset that has total population by age groups for England +#' ons_uk_population_2023 |> +#' filter(Name == "ENGLAND") |> +#' mutate(age_group = case_when( +#' as.numeric(age) <= 17 ~ "0-17", +#' as.numeric(age) >= 18 & as.numeric(age) <= 64 ~ "18-64", +#' as.numeric(age) >= 65 ~ "65+", +#' age == "90+" ~ "65+" +#' )) |> +#' group_by(age_group) |> +#' summarise(count = sum(count)) +#' +"ons_uk_population_2023" diff --git a/data-raw/ons-mortality.R b/data-raw/ons-mortality.R index 65f8c81..d206575 100644 --- a/data-raw/ons-mortality.R +++ b/data-raw/ons-mortality.R @@ -407,3 +407,5 @@ ons_mortality %>% slice(1) unlink("working_files", recursive = TRUE) + +usethis::use_data(ons_mortality, overwrite = TRUE) diff --git a/data-raw/ons_uk_population_2023.R b/data-raw/ons_uk_population_2023.R new file mode 100644 index 0000000..55978a8 --- /dev/null +++ b/data-raw/ons_uk_population_2023.R @@ -0,0 +1,36 @@ +# Source: https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/populationestimatesforukenglandandwalesscotlandandnorthernireland + +library(readxl) +library(tidyverse) +library(tidyr) + +# Load the data in +population_data_2023_f <- read_excel( + "mye23tablesuk.xlsx", # add full file path here before file name + sheet = "MYE2 - Females", + skip = 7 +) + +population_data_2023_m <- read_excel( + "mye23tablesuk.xlsx", # add full file path here before file name + sheet = "MYE2 - Males", + skip = 7 +) + + +# pivot longer +population_data_2023_f <- population_data_2023_f |> + select(!`All ages`) |> + pivot_longer(`0`:`90+`, names_to = "age", values_to = "count") + +population_data_2023_m <- population_data_2023_m |> + select(!`All ages`) |> + pivot_longer(`0`:`90+`, names_to = "age", values_to = "count") + +ons_uk_population_2023 <- bind_rows( + females = population_data_2023_f, + males = population_data_2023_m, + .id = "sex" +) + +usethis::use_data(ons_uk_population_2023, overwrite = TRUE) diff --git a/data/ons_uk_population_2023.rda b/data/ons_uk_population_2023.rda new file mode 100644 index 0000000..7f8f15b Binary files /dev/null and b/data/ons_uk_population_2023.rda differ diff --git a/man/ons_uk_population_2023.Rd b/man/ons_uk_population_2023.Rd new file mode 100644 index 0000000..6b2d37d --- /dev/null +++ b/man/ons_uk_population_2023.Rd @@ -0,0 +1,55 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ons_uk_population_2023.R +\docType{data} +\name{ons_uk_population_2023} +\alias{ons_uk_population_2023} +\title{ONS Mid-2023 Population Estimate for UK} +\format{ +Tibble with six columns +\describe{ +\item{sex}{male or female} +\item{Code}{country/geography code} +\item{Name}{country of the UK} +\item{Geography}{Country} +\item{age}{year of age} +\item{count}{the number of people in this group} +} +} +\source{ +\url{https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/populationestimatesforukenglandandwalesscotlandandnorthernireland} +} +\usage{ +data(ons_uk_population_2023) +} +\description{ +ONS Population Estimates for Mid-year 2023 +National and subnational mid-year population estimates for the UK and its +constituent countries by administrative area, age and sex (including +components of population change, median age and population density). +} +\details{ +ONS Estimates of the population for the UK, England, Wales, Scotland, and Northern Ireland +} +\examples{ +data(ons_uk_population_2023) + + +library(dplyr) +library(tidyr) + +# create a dataset that has total population by age groups for England +ons_uk_population_2023 |> + filter(Name == "ENGLAND") |> + mutate(age_group = case_when( + as.numeric(age) <= 17 ~ "0-17", + as.numeric(age) >= 18 & as.numeric(age) <= 64 ~ "18-64", + as.numeric(age) >= 65 ~ "65+", + age == "90+" ~ "65+" + )) |> + group_by(age_group) |> + summarise(count = sum(count)) + +} +\keyword{datasets} +\keyword{ons} +\keyword{population}