-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_LA_subtotals_vs_region.R
44 lines (36 loc) · 1.82 KB
/
check_LA_subtotals_vs_region.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
source("R/0_get_file_info.R")
# Sum checks --------------------------------------------------------------
# Function to compare LA subtotals match region totals.
# Outputs a table of mismatches
check_LA_region_totals <- function(indicator){
data_region <- data %>%
#filter for regional level data
dplyr::filter(geographic_level == "Regional") %>%
#select all descriptive variables and indicator of choice
dplyr::select(time_period,region_name,region_code,all_of(publication_filters),indicator) %>%
#make sure indicator is numeric
dplyr::mutate(!!indicator := as.numeric(get(indicator))) %>%
#order by year, region code then custom filter groups
dplyr::arrange(time_period,region_code,get(publication_filters))
data_la_aggregate <- data %>%
#filter for LA level data
dplyr::filter(geographic_level == "Local authority") %>%
#select descriptive varaibles and indicator of choice
dplyr::select(time_period,region_name,region_code,all_of(publication_filters),indicator) %>%
#group by all descriptive variables
dplyr::group_by(across(-c(indicator))) %>%
#summarise number of pupils for above groupings
dplyr::summarise(!!indicator := sum(as.numeric(get(indicator),na.rm = TRUE))) %>%
#order by year, region code then custom filter groups
dplyr::arrange(time_period,region_code,get(publication_filters))
#check and see if the tables differ - any rows will show which region does not add up
compare <- dplyr::setdiff(data_region,data_la_aggregate)
if (nrow(compare) == 0) {
paste0("* The LA subtotals for ", indicator, " match to the region totals")
} else {
DT::datatable(compare)
}
}
#E.g. of how you'd apply it here
avg_spend_LA_region_check <- check_LA_region_totals("average_spend")
avg_grade_LA_region_check <- check_LA_region_totals("average_grade")