Skip to content

Commit c6e4c73

Browse files
committed
Include the Mid 2022 data for small geographies
1 parent b27532f commit c6e4c73

8 files changed

Lines changed: 5851 additions & 67 deletions

population/mid_year_2022/index.Rmd

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ output:
1111
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
1212
library(tidyverse) ; library(htmltools) ; library(DT)
1313
14-
df <- read_csv("mid-year_2022_population_estimates_local_authority.csv") %>%
14+
df <- read_csv("mid-year_2022_population_estimates_ward.csv") %>%
1515
rename_all(. %>% str_to_title() %>% str_replace_all("_", " ")) %>%
1616
mutate(`Area name` = factor(`Area name`)) %>%
1717
rename_all(. %>% str_to_title())
@@ -44,16 +44,18 @@ df <- read_csv("mid-year_2022_population_estimates_local_authority.csv") %>%
4444
<td>Office for National Statistics</td>
4545
<td><a href="https://www.ons.gov.uk/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/estimatesofthepopulationforenglandandwales" target="_blank">Office for National Statistics</a></td>
4646
<td><time datetime="2022-06-30">2022-06-30</time></td>
47-
<td>Local authority</td>
47+
<td>Local authority, electoral wards, MSOAs, LSOAs and OAs</td>
4848
<td><a href="http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/" target="_blank">OGL v3.0</a></td>
4949
<td>Contains public sector information licensed under the Open Government Licence v3.0</td>
50-
<td><time datetime="2023-11-24">2023-11-24</time></td>
50+
<td><time datetime="2024-05-24">2024-05-24</time></td>
5151
</tr>
5252
</table>
5353

5454
<h2>Preview</h2>
5555
Sort, filter and export a selection of the data using the table.
5656

57+
<div id = "renderDT">
58+
5759
```{r preview}
5860
datatable(df,
5961
class = 'cell-border stripe',
@@ -68,23 +70,27 @@ datatable(df,
6870
list(extend = "csv",
6971
text = "Download selection")),
7072
deferRender = TRUE,
71-
width = NULL,
7273
scrollX = TRUE,
73-
scrollY = 120,
74+
scrollY = 300,
7475
scroller = TRUE,
7576
columnDefs = list(
76-
list(className = 'dt-left', targets = "_all"),
77-
list(visible = TRUE, targets = "_all")
77+
list(className = 'dt-left', targets = "_all")
7878
)))
7979
```
80-
<div id="tableFooterTitle">Mid-2022 population estimates for Trafford</div>
80+
</div>
81+
<div id="tableFooterTitle">Mid-2022 population estimates for Trafford's wards</div>
8182

8283
<h2>Data</h2>
8384
<span id ="downloads">Download the complete dataset using the following links:</span>
8485

8586
|File name |Format |
8687
|---|---|
88+
|[mid-year_2022_population_estimates_all_geographies.csv](mid-year_2022_population_estimates_all_geographies.csv) |CSV |
8789
|[mid-year_2022_population_estimates_local_authority.csv](mid-year_2022_population_estimates_local_authority.csv) |CSV |
90+
|[mid-year_2022_population_estimates_ward.csv](mid-year_2022_population_estimates_ward.csv) |CSV |
91+
|[mid-year_2022_population_estimates_msoa.csv](mid-year_2022_population_estimates_msoa.csv) |CSV |
92+
|[mid-year_2022_population_estimates_lsoa.csv](mid-year_2022_population_estimates_lsoa.csv) |CSV |
93+
|[mid-year_2022_population_estimates_oa.csv](mid-year_2022_population_estimates_oa.csv) |CSV |
8894

8995
<h2>Pre-processing</h2>
9096
[The R script used to process the data](pre-processing.R) can be adapted to other areas. For information about R visit the <a href="https://cran.r-project.org" target="_blank">CRAN website</a>.

population/mid_year_2022/index.html

Lines changed: 121 additions & 55 deletions
Large diffs are not rendered by default.

population/mid_year_2022/mid-year_2022_population_estimates_all_geographies.csv

Lines changed: 2782 additions & 0 deletions
Large diffs are not rendered by default.

population/mid_year_2022/mid-year_2022_population_estimates_lsoa.csv

Lines changed: 418 additions & 0 deletions
Large diffs are not rendered by default.

population/mid_year_2022/mid-year_2022_population_estimates_msoa.csv

Lines changed: 85 additions & 0 deletions
Large diffs are not rendered by default.

population/mid_year_2022/mid-year_2022_population_estimates_oa.csv

Lines changed: 2215 additions & 0 deletions
Large diffs are not rendered by default.

population/mid_year_2022/mid-year_2022_population_estimates_ward.csv

Lines changed: 64 additions & 0 deletions
Large diffs are not rendered by default.

population/mid_year_2022/pre_processing.R

Lines changed: 152 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)