/R/data/get_popn_nhs.R

https://gitlab.com/pjbirrell/real-time-mcmc · R · 58 lines · 45 code · 6 blank · 7 comment · 2 complexity · 53e48fc597280dd5fefaf77903765b8e MD5 · raw file

  1. library(readr)
  2. library(tidyr)
  3. library(dplyr)
  4. source("utils.R")
  5. dir.data <- ("../../data/population")
  6. ## Load in the population data in long format
  7. pop <- read_csv(build.data.filepath(subdir = "", "pop_long_nhsrlo_2018_syoa.csv"))
  8. ## Load in a lookup table to get the region names, rather than codes.
  9. lookup <- read_csv(build.data.filepath(subdir = "", "nhs_region_lookup.csv"))
  10. lookup <- unique(lookup[, c(3, 4)])
  11. ## If we're aggregating by age, get the aggregation being used or set to default.
  12. if(!exists("age.agg")){
  13. age.agg <- c(0, 1, 5, 15, 25, 45, 65, 91)
  14. age.labs <- c("<1yr","1-4","5-14","15-24","25-44","45-64","65+")
  15. }
  16. ## Put data into wide format - so it resembles the data sent by the ONS.
  17. pop <- pop %>%
  18. filter(Sex == 4) %>%
  19. pivot_wider(id_cols = NHSERApr19CD, names_from = Age, values_from = Population)
  20. pop <- pop %>%
  21. mutate("All ages" = apply(pop[, -1], 1, sum)) %>%
  22. inner_join(.,lookup) %>%
  23. rename(Code = NHSERApr19CD,
  24. Name = NHSERApr19NM) %>%
  25. mutate(Geography = "NHS Region") %>%
  26. select(Code,
  27. Name,
  28. Geography,
  29. "All ages",
  30. everything())
  31. ## Put back into long format with age aggregation.
  32. ## Long format...
  33. regions <- pop %>%
  34. pivot_longer(-(1:4), names_to = "Age", values_to = "popn")
  35. regions$Age <- as.integer(regions$Age)
  36. regions$Grp <- cut(regions$Age, age.agg, age.labs,right=FALSE,ordered_result=T)
  37. regions.short <- aggregate(regions$popn, by = list(Region=regions$Name,Age=regions$Grp), FUN = sum, drop=TRUE)
  38. regions.short$Region <- gsub(" ", "_", regions.short$Region)
  39. ## Define some aggregate regions that may get used...
  40. nhs.regions <- list("London" = "London",
  41. "Outside_London" = c("North East and Yorkshire", "North West", "Midlands", "East of England", "South East", "South West"),
  42. "UNITED_KINGDOM" = "UNITED KINGDOM",
  43. "ENGLAND" = unlist(lookup[, 2]),
  44. "East_of_England" = "East of England",
  45. "London" = "London",
  46. "Midlands" = "Midlands",
  47. "North_East_and_Yorkshire" = "North East and Yorkshire",
  48. "North_West" = "North West",
  49. "South_East" = "South East",
  50. "South_West" = "South West"
  51. )
  52. save(pop, nhs.regions, file = build.data.filepath(subdir = "", "pop_nhs.RData"))