-
Notifications
You must be signed in to change notification settings - Fork 7
/
05.2b_school_scenario_building.R
51 lines (40 loc) · 3.19 KB
/
05.2b_school_scenario_building.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
#SET UP
rm(list = ls())
source("00_setup_and_funs.R")
memory.limit(size=1000000)
purpose <- "school"
purpose_private <- paste0(purpose, "_private")
geography <- "lsoa"
if(!dir.exists(file.path(path_outputs_national, purpose))) { dir.create(file.path(path_outputs_national, purpose)) }
if(!dir.exists(file.path(path_outputs_national, purpose, geography))) { dir.create(file.path(path_outputs_national, purpose, geography)) }
if(!dir.exists(file.path(path_outputs_national, purpose_private))) { dir.create(file.path(path_outputs_national, purpose_private)) }
if(!dir.exists(file.path(path_outputs_national, purpose_private, geography))) { dir.create(file.path(path_outputs_national, purpose_private, geography)) }
#########################
### ROUND AND SAVE SCENARIO ATTRIBUTE (this will be done in previous stage when move stata to R)
#########################
## OPEN ATTRIBUTE DATA
z_all_attributes <- read_csv(file.path(path_temp_scenario, purpose, geography, "z_all_attributes_unrounded.csv"))
d_all_attributes <- read_csv(file.path(path_temp_scenario, purpose, geography, "d_all_attributes_unrounded.csv"))
z_all_attributes_private <- read_csv(file.path(path_temp_scenario, purpose, geography, "z_all_attributes_private_unrounded.csv"))
d_all_attributes_private <- read_csv(file.path(path_temp_scenario, purpose, geography, "d_all_attributes_private_unrounded.csv"))
lad_attributes <- read_csv(file.path(path_temp_scenario, purpose, "lad_all_attributes_unrounded.csv"))
## OPEN CODEBOOKS
z_codebook <- read_csv(file.path(path_codebooks, purpose, "z_codebook.csv"))
d_codebook <- read_csv(file.path(path_codebooks, purpose, "d_codebook.csv"))
lad_codebook <- read_csv(file.path(path_codebooks, purpose, "lad_codebook.csv"))
## ROUND THE PUBLIC DATA (ONLY), SUBSET TO CODEBOOK VARIABLES, SAVE
z_all_attributes <- z_all_attributes[order(z_all_attributes$geo_name),] # A bodge: sort so as not to have at the top an LSOA that has 0 cars in, as this messes up rounding. Correct 2 lines later
z_all_attributes <- as.data.frame(apply(z_all_attributes, c(2), round_df), stringsAsFactors = F)
z_all_attributes <- z_all_attributes[order(z_all_attributes$geo_code),]
z_all_attributes <- z_all_attributes[z_codebook$`Variable name`]
write_csv(z_all_attributes, file.path(path_outputs_national, purpose, geography, "z_all_attributes.csv"))
z_all_attributes_private <- z_all_attributes_private[z_codebook$`Variable name`]
write_csv(z_all_attributes_private, file.path(path_outputs_national, purpose_private, geography, "z_all_attributes.csv"))
d_all_attributes <- as.data.frame(apply(d_all_attributes, c(2), round_df), stringsAsFactors = F)
d_all_attributes <- d_all_attributes[d_codebook$`Variable name`]
write_csv(d_all_attributes, file.path(path_outputs_national, purpose, geography, "d_all_attributes.csv"))
d_all_attributes_private <- d_all_attributes_private[d_codebook$`Variable name`]
write_csv(d_all_attributes_private, file.path(path_outputs_national, purpose_private, geography, "d_all_attributes.csv"))
lad_attributes <- as.data.frame(apply(lad_attributes, c(2), round_df), stringsAsFactors = F)
lad_attributes <- lad_attributes[lad_codebook$`Variable name`]
write_csv(lad_attributes, file.path(path_outputs_national, purpose, "lad_attributes.csv"))