Skip to content

Commit 2c98c18

Browse files
committed
add conditional formatting to number of sampled addresses per postcode & fix error in SHS sample size check
1 parent 32efcf8 commit 2c98c18

5 files changed

Lines changed: 68 additions & 10 deletions

File tree

functions/qa_export.R

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,48 @@ qa_export <- function(list_df, survey){
133133

134134
# udprn ----
135135

136-
# add red/green colouring to columns containing the word 'udprn'
136+
# add red colouring to columns containing the word 'udprn'
137137
# red if udprn isn't 0 (i.e., udprn has been previously sampled)
138138

139139
udprn <- grep("^udprn", colnames(data))
140-
if(names(list_df[i]) == "previously.sampled.udprn"){
140+
if(names(list_df[i]) == "previously.sampled.udprn" &
141+
nrow(list_df[["previously.sampled.udprn"]]) != 0){
141142
conditionalFormatting(wb = wb, sheet = sheet,
142143
cols = udprn,
143144
rows = 2:(nrow(data)+1),
144145
type = "expression",
145146
rule = ' != 0',
146147
style = redstyle)
147148
}
149+
150+
# sampled postcodes ----
151+
152+
# add red/green colouring to sampled postcodes
153+
# red if more than 10 addresses were sampled in one postcode
154+
155+
if(names(list_df[i]) == "sampled.postcodes"){
156+
conditionalFormatting(wb = wb,
157+
sheet = sheet,
158+
cols = 2,
159+
rows = 2:(nrow(data)+1),
160+
type = "expression",
161+
rule = ' > 10',
162+
style = redstyle)
163+
}
164+
165+
if(names(list_df[i]) == "sampled.postcodes"){
166+
conditionalFormatting(wb = wb,
167+
sheet = sheet,
168+
cols = 2,
169+
rows = 2:(nrow(data)+1),
170+
type = "expression",
171+
rule = ' <= 10',
172+
style = greenstyle)
148173
}
149174

175+
176+
}
177+
150178
# export to Excel file
151179
path <- eval(as.name(paste0(survey, ".path")))
152180
saveWorkbook(wb, file = paste0(path,

scripts/01_paf.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ rawpaf <- read_csv(infilenm.path,
4747
Locality, Town, Postcode, PrintAddress,
4848
Multi_occupancy, CouncilArea, UDPRN,
4949
YCOORD, XCOORD, "2011Datazone", LACode,
50-
UPRN, CouncilTaxBand)) %>%
50+
UPRN, CouncilTaxBand),
51+
show_col_types = FALSE) %>%
5152
clean_names_modified() %>%
5253
mutate(datazone = substr(x2011datazone, 1, 9),
5354
udprn = as.numeric(udprn))
@@ -233,8 +234,14 @@ residential <- shes.strata %>%
233234
shes_y2 = ifelse(shes_set == "B", 1, 0),
234235
shes_y3 = ifelse(shes_set == "C", 1, 0),
235236
shes_y4 = ifelse(shes_set == "D", 1, 0)) %>%
236-
right_join(dz_info) %>%
237-
right_join(residential)
237+
right_join(dz_info,
238+
by = join_by(dz11),
239+
suffix = c('.x', '')) %>%
240+
select(-contains('.x')) %>%
241+
right_join(residential,
242+
by = join_by(dz11),
243+
suffix = c('.x', '')) %>%
244+
select(-contains('.x'))
238245
nrow(residential)
239246

240247
# Remove observations with infrequent la_scode, la_code and la combination
@@ -246,7 +253,10 @@ pafaux <- residential %>%
246253

247254
# Merge residential with pafaux
248255
paf_check <- residential %>%
249-
left_join(pafaux)
256+
left_join(pafaux,
257+
by = join_by(la_code),
258+
suffix = c('', '.y')) %>%
259+
select(-contains('.y'))
250260
nrow(paf_check)
251261

252262
# Harmonise la and la_code variables

scripts/03_shs_sampling.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ shs.contractorsample <- shs.mainsample %>%
135135
# Merge with main sample
136136
right_join(shs.mainsample,
137137
by = join_by(udprn),
138-
suffix = c('', '.y')) %>%
139-
select(-contains('.y')) %>%
138+
suffix = c('.x', '')) %>%
139+
select(-contains('.x')) %>%
140140

141141
# Replace NAs in houseconditionflag with 0
142142
mutate(houseconditionflag = replace_na(houseconditionflag, 0))

scripts/04_shes_checking.R

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,25 @@ table(shes.biomod.frameandmatchedsample$health_board,
202202

203203
shes.biomod.frameandmatchedsample %>% count(health_board)
204204

205+
### 13 - Check urban/rural by core in contractor sample ----
206+
207+
core.qa <- contractor.sample %>%
208+
filter(core == 1) %>%
209+
group_by(la, sample_type) %>%
210+
summarise(mean = mean(dz11_urbrur2020),
211+
.groups = "drop") %>%
212+
pivot_wider(names_from = sample_type,
213+
values_from = mean) %>%
214+
ungroup() %>%
215+
mutate(diff = .[[3]] - .[[2]])
216+
217+
if(any(core.qa$diff < -paf_sample.threshold | core.qa$diff > paf_sample.threshold)){
218+
warning(print(paste0("For at least one local authority,",
219+
"the difference in urban/rural classification",
220+
"between core bio and core non-bio",
221+
"is greater than expected")))
222+
}
223+
205224
### 12 - Check data zones in contractor sample ----
206225

207226
# Add message to inform user about progress
@@ -250,7 +269,8 @@ qa <- list(contractor.sample = contractor.sample,
250269
contractor.datazone = contractor.datazone.qa,
251270
contractor.simdq.la = contractor.simdq.qa,
252271
contractor.urbrur = contractor.urbrur.qa[[2]],
253-
contractor.urbrur.la = contractor.urbrur.qa[[1]])
272+
contractor.urbrur.la = contractor.urbrur.qa[[1]],
273+
contractor.urbrur.core = core.qa)
254274

255275
# Export to Excel
256276

scripts/04_shs_checking.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ survey <- "shs"
2525
source(here::here("scripts", "00_setup.R"))
2626

2727
# Add message to inform user about progress
28-
cat(crayon::bold("\nExecute checking script"))
28+
message(title("Execute checking script"))
2929

3030
### 1 - Import data ----
3131

0 commit comments

Comments
 (0)