Skip to content

Commit

Permalink
Merge pull request #95 from jacgrout/main
Browse files Browse the repository at this point in the history
Added a population dataset
  • Loading branch information
tomjemmett authored Oct 18, 2024
2 parents 0706456 + a47a03e commit 9236e43
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
48 changes: 48 additions & 0 deletions R/ons_uk_population_2023.R
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions data-raw/ons-mortality.R
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,5 @@ ons_mortality %>%
slice(1)

unlink("working_files", recursive = TRUE)

usethis::use_data(ons_mortality, overwrite = TRUE)
36 changes: 36 additions & 0 deletions data-raw/ons_uk_population_2023.R
Original file line number Diff line number Diff line change
@@ -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)
Binary file added data/ons_uk_population_2023.rda
Binary file not shown.
55 changes: 55 additions & 0 deletions man/ons_uk_population_2023.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9236e43

Please sign in to comment.