-
Notifications
You must be signed in to change notification settings - Fork 0
/
censusData_smallToLargeGeogs_5_censusTo1991aggPCS.R
290 lines (202 loc) · 10.4 KB
/
censusData_smallToLargeGeogs_5_censusTo1991aggPCS.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#Stick time-consistent data (same columns) through the mill to get same zone re-assignment
source("Function_DataFromSmalltoLargeGeog.R")
#Large zones that all data will have in common
# lrg <- readOGR(dsn="C:/Data/MapPolygons/Scotland/1991/Scotland_postcodesectors_1991",
# layer="scotland_pcs_1991_uniqueIDsperRow_LochsRemoved")
#Aggregated PCSs 822 zones
lrg <- readOGR(dsn="C:/Data/MapPolygons/Scotland/1991/pseudoPCS_aggregated4CorrectCount",
layer="pseudoPCS_aggregated4CorrectCount")
#~~~~COB~~~~----
#~~~~~~~~
#1971----
its <- readOGR(dsn="Intersects/Scots_5_census_91_PCSzeroCounts_targetGeog",
layer="71EDsTo91PCSzeroCount")
dta <- read.csv("VariableCoding/CountryOfBirth_fiveCensusRecodes/71.csv")
its_smallZoneIDColumn <- 1
its_largeZoneIDColumn <- 2
dta_zoneIDcolumn <- 1
lrgID <- 1#cos it won't always match lrg name in intersect cos of clashes e.g. "label_2"
datacols <- c(2:15)#from dta
result <- moveData(its,dta,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols)
#save result!
# writeOGR(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/CountryOfBirth",
# "1971_CoB_from_71EDs_to_91_aggPostcodeSectors", driver="ESRI Shapefile", overwrite_layer = T)
#better column abbreviation
writeSpatialShape(result,"StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/CountryOfBirth/1971_CoB_from_71EDs_to_91_aggPostcodeSectors")
#~~~~~~~
#1981----
its <- readOGR(dsn="Intersects/Scots_5_census_91_PCSzeroCounts_targetGeog",
layer="81EDsTo91PCSzeroCount")
dta <- read.csv("VariableCoding/CountryOfBirth_fiveCensusRecodes/81.csv")
its_smallZoneIDColumn <- 1
its_largeZoneIDColumn <- 2
dta_zoneIDcolumn <- 1
lrgID <- 1#cos it won't always match lrg name in intersect cos of clashes e.g. "label_2"
datacols <- c(2:15)#from dta
result <- moveData(its,dta,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols)
#save result!
# writeOGR(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/CountryOfBirth",
# "1981_CoB_from_81EDs_to_91_aggPostcodeSectors", driver="ESRI Shapefile", overwrite_layer = T)
#better column abbreviation
writeSpatialShape(result,"StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/CountryOfBirth/1981_CoB_from_81EDs_to_91_aggPostcodeSectors")
#~~~~~~~~~~~
#2001----
#Can use intersect file from 3 census
its <- readOGR(dsn="Intersects/scots_3censusLBS_91PCSzeroCountIsTargetGeog",
layer="2001OAsTo91PCSzerocount")
dta <- read.csv("VariableCoding/CountryOfBirth_fiveCensusRecodes/01.csv")
its_smallZoneIDColumn <- 1
its_largeZoneIDColumn <- 2
dta_zoneIDcolumn <- 1
lrgID <- 1#cos it won't always match lrg name in intersect cos of clashes e.g. "label_2"
datacols <- c(2:15)#from dta
result <- moveData(its,dta,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols)
#save result!
# writeOGR(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/CountryOfBirth",
# "2001_CoB_from_01OAs_to_91_aggPostcodeSectors", driver="ESRI Shapefile", overwrite_layer = T)
#better column abbreviation
writeSpatialShape(result,"StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/CountryOfBirth/2001_CoB_from_01OAs_to_91_aggPostcodeSectors")
#~~~~~~~~~~~
#2011----
its <- readOGR(dsn="Intersects/scots_3censusLBS_91PCSzeroCountIsTargetGeog",
layer="2011OAsTo91PCSzerocount")
#err...
#df <- data.frame(its)
dta <- read.csv("VariableCoding/CountryOfBirth_fiveCensusRecodes/11.csv")
its_smallZoneIDColumn <- 2
its_largeZoneIDColumn <- 21
dta_zoneIDcolumn <- 1
lrgID <- 1#cos it won't always match lrg name in intersect cos of clashes e.g. "label_2"
datacols <- c(2:15)#from dta
result <- moveData(its,dta,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols)
#save result!
# writeOGR(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/CountryOfBirth",
# "2011_CoB_from_11OAs_to_91_aggPostcodeSectors", driver="ESRI Shapefile", overwrite_layer = T)
#better column abbreviation
writeSpatialShape(result,"StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/CountryOfBirth/2011_CoB_from_11OAs_to_91_aggPostcodeSectors")
#~~~~~~~~
#1991 done in 91_PCSzeroCountZones_agg...
#Most other variables (for 91 to 11) already done for 3 census
#But some bits left to do:
#~~~~EMPLOYMENT~~~~----
#71/81 Copied across and tweaked from household_n_employmentCensusDataTidy.R
ea71 <- read.csv("1971/Scotland/Scots_71_economicallyActive.csv")
ea81 <- read.csv("1981/Scotland/Scots_1981_economicallyActive_allTotals.csv")
#Ready to be stuffed through the geog re-assign
#Get large geog that re-assigned data will be attached to.
lrg <- readOGR(dsn="C:/Data/MapPolygons/Scotland/1991/pseudoPCS_aggregated4CorrectCount",
layer="pseudoPCS_aggregated4CorrectCount")
#1971----
#Total persons EA and total persons not in employment all that's required.
#Via eyeballing '71 CASWEB...
#First job: sum male/female (rows)
ea71b <- data.frame(ZoneCode = ea71[,1],
total = apply(ea71[,c(3:5)],1,sum),
working = apply(ea71[,c(6:8)],1,sum),
Unempl = apply(ea71[,c(9:11)],1,sum),
sick = apply(ea71[,c(12:14)],1,sum)
)
#"Total" doesn't seem quite to be the total of the others
#Reckon I'll use the ratio of "working" and "seeking work".
#And presume "sick" means "long-term sick".
#Though this is an EA table so that could be wrong. The sick numbers seem too high though...?
#ratio of working and seeking work, then...
ea71b$EA <- ea71b$working + ea71b$Unempl
ea71b$percentEmployed <- (1 - (ea71b$Unempl/ea71b$EA)) * 100
ea71b <- ea71b[,c(1,4,6:7)]
#Get the intersect geog:
its <- readOGR(dsn="Intersects/Scots_5_census_91_PCSzeroCounts_targetGeog",
layer="71EDsTo91PCSzeroCount")
its_smallZoneIDColumn <- 1
its_largeZoneIDColumn <- 2
dta_zoneIDcolumn <- 1
lrgID <- 1#cos it won't always match lrg name in intersect cos of clashes e.g. "label_2"
datacols <- c(2:4)#from dta
result <- moveData(its,ea71b,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols)
#debugonce(moveData(its,hse71,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols))
#Oops. %employed again - do AFTER geog summing!
result@data$percentEmployed <- (1 - (result@data$Unempl/result@data$EA)) * 100
#save result!
writeOGR(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/Employment",
"1971_econActive_from_71EDs_to_91_aggPostcodeSectors", driver="ESRI Shapefile", overwrite_layer = T)
#1981----
#Total persons EA and total persons not in employment all that's required.
#Via eyeballing '71 CASWEB...
#First job: sum male/female (rows)
ea81b <- data.frame(ZoneCode = ea81[,1],
EA = ea81[,2],
Unempl = ea81[,12]
)
#Lrg same as above
#Get the intersect geog:
its <- readOGR(dsn="Intersects/Scots_5_census_91_PCSzeroCounts_targetGeog",
layer="81EDsTo91PCSzeroCount")
#spaces in '81 IDs
ea81b$ZoneCode <- trimws(ea81b$ZoneCode,which = 'both')
unique(its@data$label) %in% unique(ea81b$ZoneCode) %>% table#better!
its_smallZoneIDColumn <- 1
its_largeZoneIDColumn <- 2
dta_zoneIDcolumn <- 1
lrgID <- 1#cos it won't always match lrg name in intersect cos of clashes e.g. "label_2"
datacols <- c(2:3)#from dta
result <- moveData(its,ea81b,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols)
#debugonce(moveData(its,hse71,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols))
#%employed - do AFTER geog summing!
result@data$percentEmployed <- (1 - (result@data$Unempl/result@data$EA)) * 100
#save result!
writeOGR(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/Employment",
"1981_econActive_from_81EDs_to_91_aggPostcodeSectors", driver="ESRI Shapefile", overwrite_layer = T)
#~~~~YEAR OF ARRIVAL~~~~----
#tidy the file
# yoa <- read_csv('2011/Scotland/OutputAreaStd_ALL/QS801SCtidy.csv')
# yoa[yoa=='-'] <- 0
#write_csv(yoa,'2011/Scotland/OutputAreaStd_ALL/QS801SCtidyZeroes.csv')
yoa <- read_csv('2011/Scotland/OutputAreaStd_ALL/QS801SCtidyZeroes.csv')
#Do some summing to different values
names(yoa)
#keep:
#born in the UK
#arrived before 71
#71/81
#81/91
#91/01
#01/11
yoa2 <- yoa[,c(1,3)]
yoa2$arrivedBefore1971 <- apply(yoa[,c(4:7)],1,sum)
yoa2$arrived1971to80 <- yoa$`Arrived 1971-1980`
yoa2$arrived1981to90 <- yoa$`Arrived 1981-1990`
yoa2$arrived1991to2000 <- yoa$`Arrived 1991-2000`
yoa2$arrived2001to2011 <- apply(yoa[,c(11:14)],1,sum)
write_csv(yoa2,'2011/Scotland/OutputAreaStd_ALL/QS801SCtidyZeroes_reducedCols.csv')
#yoa <- read_csv('2011/Scotland/OutputAreaStd_ALL/QS801SCtidyZeroes_reducedCols.csv')
#~~~~~~~~~~~~~~~~~~~~~~~~~~
#Ready for geog aggregation
its <- readOGR(dsn="Intersects/scots_3censusLBS_91PCSzeroCountIsTargetGeog",
layer="2011OAsTo91PCSzerocount")
dta <- read.csv('2011/Scotland/OutputAreaStd_ALL/QS801SCtidyZeroes_reducedCols.csv')
#dta[,c(2:7)] <- sapply(dta[,c(2:7)],as.numeric)
dta[is.na(dta)] <- 0
apply(dta[,c(2:7)],2,sum)
table(dta$zonecode %in% its@data$code)
its_smallZoneIDColumn <- 2
its_largeZoneIDColumn <- 21
dta_zoneIDcolumn <- 1
lrgID <- 1#cos it won't always match lrg name in intersect cos of clashes e.g. "label_2"
datacols <- c(2:7)#from dta
result <- moveData(its,dta,lrg,its_smallZoneIDColumn,its_largeZoneIDColumn,dta_zoneIDcolumn,lrgID,datacols)
chk <- data.frame(result)
apply(chk[,c(3:8)],2,sum)
#save result!
# writeOGR(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/YearOfArrival",
# "2011_YearOfArrival_from_11OAs_to_91_aggPostcodeSectors", driver="ESRI Shapefile", overwrite_layer = T)
writeSpatialShape(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/YearOfArrival/2011_YearOfArrival_from_11OAs_to_91_aggPostcodeSectors.shp")
#A version that's proportions of the total zone pop
#And before/after 2001
chk$totalPop <- chk$Born.in.the.UK + apply(chk[,c(4:8)],1,sum)
chk$arrivedBefore2001 <- apply(chk[,c(4:7)],1,sum)
chk$arrivedBefore2001AsPercentOfPop <- (chk$arrivedBefore2001/chk$totalPop)*100
chk$arrivedAfter2001AsPercentOfPop <- (chk$arrived2001to2011/chk$totalPop)*100
#ratio of the two
chk$ratioAfterOverBeforePercents <- chk$arrivedAfter2001AsPercentOfPop/chk$arrivedBefore2001AsPercentOfPop
result@data <- chk[,c(1,3,10,8,12,11,13)]
writeSpatialShape(result, "StitchOutputs/Scotland/LBS_postcodeSector_5Census_raw/YearOfArrival/2011_YearOfArrival_from_11OAs_to_91_aggPostcodeSectors_ProportionsOfTotalPop.shp")