-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from jacgrout/main
Added a population dataset
- Loading branch information
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.