diff --git a/R/ons_uk_population_2023.R b/R/ons_uk_population_2023.R index 52769e4..7a006c6 100644 --- a/R/ons_uk_population_2023.R +++ b/R/ons_uk_population_2023.R @@ -35,14 +35,14 @@ #' #' # 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)) -#' -#' +#' 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_uk_population_2023.R b/data-raw/ons_uk_population_2023.R new file mode 100644 index 0000000..7d00665 --- /dev/null +++ b/data-raw/ons_uk_population_2023.R @@ -0,0 +1,34 @@ +# 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") + +population_data_combined <- bind_rows( + females = population_data_2023_f, + males = population_data_2023_m, + .id = "sex" +)