@@ -24,12 +24,160 @@ mid2022M <- read_xlsx(tmp, sheet = 9, skip = 7) %>%
2424 filter(Code %in% c(" E08000009" )) %> %
2525 mutate(sex = " Males" )
2626
27- df <- bind_rows(mid2022P , mid2022F , mid2022M ) %> %
27+ df_la <- bind_rows(mid2022P , mid2022F , mid2022M ) %> %
2828 mutate(geography = " Local authority" ,
2929 period = " 2022-06-30" ) %> %
3030 mutate(aged_0_to_15 = rowSums(select(. , `0` : `15` )),
3131 aged_16_to_64 = rowSums(select(. , `16` : `64` )),
3232 aged_65_and_over = rowSums(select(. , `65` : `90+` ))) %> %
33- select(period , area_code = Code , area_name = Name , geography , sex , all_ages = `All ages` , aged_0_to_15 , aged_16_to_64 , aged_65_and_over , everything(), - Geography )
34-
35- write_csv(df , " mid-year_2022_population_estimates_local_authority.csv" )
33+ select(period , area_code = Code , area_name = Name , geography , sex , all_ages = `All ages` , aged_0_to_15 , aged_16_to_64 , aged_65_and_over , everything(), - Geography )
34+
35+ write_csv(df_la , " mid-year_2022_population_estimates_local_authority.csv" )
36+
37+
38+ # Wards
39+
40+ # Source: ONS
41+ # URL: https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/middlesuperoutputareamidyearpopulationestimates
42+ # Licence: Open Government Licence 3.0
43+
44+ tmp <- tempfile(fileext = " .xlsx" )
45+
46+ GET(url = " https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/wardlevelmidyearpopulationestimatesexperimental/mid2021andmid2022/sapewardstablefinal.xlsx" ,
47+ write_disk(tmp ))
48+
49+ df_ward <- read_xlsx(tmp , sheet = 8 , skip = 3 ) %> %
50+ filter(`LAD 2023 Code` == " E08000009" ) %> %
51+ rename(area_code = `Ward 2023 Code` , area_name = `Ward 2023 Name` ) %> %
52+ pivot_longer(Total : M90 , names_to = " age" , values_to = " value" ) %> %
53+ mutate(period = " 2022-06-30" ) %> %
54+ filter(age != " Total" ) %> %
55+ separate(age , into = c(" sex" , " age" ), sep = 1 ) %> %
56+ mutate(sex = case_match(sex ," F" ~ " Females" ," M" ~ " Males" )) %> %
57+ pivot_wider(names_from = " sex" , values_from = value ) %> %
58+ mutate(Persons = Females + Males ) %> %
59+ pivot_longer(Females : Persons , names_to = " sex" , values_to = " value" ) %> %
60+ pivot_wider(values_from = " value" , names_from = " age" ) %> %
61+ select(- `LAD 2023 Code` , - `LAD 2023 Name` ) %> %
62+ rename(`90+` = `90` ) %> %
63+ mutate(geography = " Ward" ) %> %
64+ mutate(all_ages = rowSums(select(. , `0` : `90+` )),
65+ aged_0_to_15 = rowSums(select(. , `0` : `15` )),
66+ aged_16_to_64 = rowSums(select(. , `16` : `64` )),
67+ aged_65_and_over = rowSums(select(. , `65` : `90+` ))) %> %
68+ select(period , area_code , area_name , geography , sex , all_ages , aged_0_to_15 , aged_16_to_64 , aged_65_and_over , everything())
69+
70+ write_csv(df_ward , " mid-year_2022_population_estimates_ward.csv" )
71+
72+ # MSOAs
73+
74+ # House of Commons Library MSOA Names
75+ # URL: https://visual.parliament.uk/msoanames
76+
77+ lookup <- read_csv(" https://houseofcommonslibrary.github.io/msoanames/MSOA-Names-Latest.csv" ) %> %
78+ filter(Laname == " Trafford" ) %> %
79+ select(area_code = msoa11cd , area_name = msoa11hclnm )
80+
81+ # Source: ONS
82+ # URL: https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/middlesuperoutputareamidyearpopulationestimates
83+ # Licence: Open Government Licence 3.0
84+
85+ tmp <- tempfile(fileext = " .xlsx" )
86+
87+ GET(url = " https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/middlesuperoutputareamidyearpopulationestimates/mid2021andmid2022/sapemsoasyoatablefinal.xlsx" ,
88+ write_disk(tmp ))
89+
90+ df_msoa <- read_xlsx(tmp , sheet = 6 , skip = 3 ) %> %
91+ filter(`LAD 2021 Code` == " E08000009" ) %> %
92+ rename(area_code = `MSOA 2021 Code` ) %> %
93+ pivot_longer(Total : M90 , names_to = " age" , values_to = " value" ) %> %
94+ mutate(period = " 2022-06-30" ) %> %
95+ filter(age != " Total" ) %> %
96+ separate(age , into = c(" sex" , " age" ), sep = 1 ) %> %
97+ mutate(sex = case_match(sex ," F" ~ " Females" ," M" ~ " Males" )) %> %
98+ pivot_wider(names_from = " sex" , values_from = value ) %> %
99+ mutate(Persons = Females + Males ) %> %
100+ pivot_longer(Females : Persons , names_to = " sex" , values_to = " value" ) %> %
101+ pivot_wider(values_from = " value" , names_from = " age" ) %> %
102+ select(- `LAD 2021 Code` , - `LAD 2021 Name` ) %> %
103+ rename(`90+` = `90` ) %> %
104+ mutate(geography = " MSOA" ) %> %
105+ mutate(all_ages = rowSums(select(. , `0` : `90+` )),
106+ aged_0_to_15 = rowSums(select(. , `0` : `15` )),
107+ aged_16_to_64 = rowSums(select(. , `16` : `64` )),
108+ aged_65_and_over = rowSums(select(. , `65` : `90+` ))) %> %
109+ left_join(lookup , by = " area_code" ) %> %
110+ select(period , area_code , area_name , geography , sex , all_ages , aged_0_to_15 , aged_16_to_64 , aged_65_and_over , everything(),- `MSOA 2021 Name` )
111+
112+ write_csv(df_msoa , " mid-year_2022_population_estimates_msoa.csv" )
113+
114+ # LSOA
115+
116+ # Source: ONS
117+ # URL: https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/lowersuperoutputareamidyearpopulationestimates
118+ # Licence: Open Government Licence 3.0
119+
120+ tmp <- tempfile(fileext = " .xlsx" )
121+
122+ GET(url = " https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/lowersuperoutputareamidyearpopulationestimates/mid2021andmid2022/sapelsoasyoatablefinal.xlsx" ,
123+ write_disk(tmp ))
124+
125+ df_lsoa <- read_xlsx(tmp , sheet = 6 , skip = 3 ) %> %
126+ filter(`LAD 2021 Code` == " E08000009" ) %> %
127+ rename(area_code = `LSOA 2021 Code` , area_name = `LSOA 2021 Name` ) %> %
128+ pivot_longer(Total : M90 , names_to = " age" , values_to = " value" ) %> %
129+ mutate(period = " 2022-06-30" ) %> %
130+ filter(age != " Total" ) %> %
131+ separate(age , into = c(" sex" , " age" ), sep = 1 ) %> %
132+ mutate(sex = case_match(sex ," F" ~ " Females" ," M" ~ " Males" )) %> %
133+ pivot_wider(names_from = " sex" , values_from = value ) %> %
134+ mutate(Persons = Females + Males ) %> %
135+ pivot_longer(Females : Persons , names_to = " sex" , values_to = " value" ) %> %
136+ pivot_wider(values_from = " value" , names_from = " age" ) %> %
137+ select(- `LAD 2021 Code` , - `LAD 2021 Name` ) %> %
138+ rename(`90+` = `90` ) %> %
139+ mutate(geography = " LSOA" ) %> %
140+ mutate(all_ages = rowSums(select(. , `0` : `90+` )),
141+ aged_0_to_15 = rowSums(select(. , `0` : `15` )),
142+ aged_16_to_64 = rowSums(select(. , `16` : `64` )),
143+ aged_65_and_over = rowSums(select(. , `65` : `90+` ))) %> %
144+ select(period , area_code , area_name , geography , sex , all_ages , aged_0_to_15 , aged_16_to_64 , aged_65_and_over , everything())
145+
146+ write_csv(df_lsoa , " mid-year_2022_population_estimates_lsoa.csv" )
147+
148+
149+ # OA
150+
151+ # Source: ONS
152+ # URL: https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/censusoutputareapopulationestimatessupportinginformation
153+ # Licence: Open Government Licence 3.0
154+
155+ tmp <- tempfile(fileext = " .xlsx" )
156+
157+ GET(url = " https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/censusoutputareapopulationestimatessupportinginformation/mid2022/sapeoatablefinal2022v2.xlsx" ,
158+ write_disk(tmp ))
159+
160+ df_oa <- read_xlsx(tmp , sheet = 5 , skip = 3 ) %> %
161+ filter(`LAD 2021 Code` == " E08000009" ) %> %
162+ rename(area_code = `OA 2021 Code` ) %> %
163+ pivot_longer(Total : M90 , names_to = " age" , values_to = " value" ) %> %
164+ mutate(period = " 2022-06-30" ) %> %
165+ filter(age != " Total" ) %> %
166+ separate(age , into = c(" sex" , " age" ), sep = 1 ) %> %
167+ mutate(sex = case_match(sex ," F" ~ " Females" ," M" ~ " Males" )) %> %
168+ pivot_wider(names_from = " sex" , values_from = value ) %> %
169+ mutate(Persons = Females + Males ) %> %
170+ pivot_longer(Females : Persons , names_to = " sex" , values_to = " value" ) %> %
171+ pivot_wider(values_from = " value" , names_from = " age" ) %> %
172+ select(- `LAD 2021 Code` , - `LAD 2021 Name` ) %> %
173+ rename(`90+` = `90` ) %> %
174+ mutate(geography = " OA" , area_name = area_code ) %> %
175+ mutate(all_ages = rowSums(select(. , `0` : `90+` )),
176+ aged_0_to_15 = rowSums(select(. , `0` : `15` )),
177+ aged_16_to_64 = rowSums(select(. , `16` : `64` )),
178+ aged_65_and_over = rowSums(select(. , `65` : `90+` ))) %> %
179+ select(period , area_code , area_name , geography , sex , all_ages , aged_0_to_15 , aged_16_to_64 , aged_65_and_over , everything())
180+
181+ write_csv(df_oa , " mid-year_2022_population_estimates_oa.csv" )
182+
183+ write_csv(bind_rows(df_la ,df_ward ,df_lsoa ,df_msoa ,df_oa ), " mid-year_2022_population_estimates_all_geographies.csv" )
0 commit comments