-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqa_tables.R
55 lines (42 loc) · 1.7 KB
/
qa_tables.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
#' Take the Difference of a Matching Column in Two Tables
#'
#' This function returns a vector of differences corresponding to a specified column
#' in two data frames. It will only consider rows present in both data
#' sets based on the first column.
#' @param table_1 First table
#' @param table_2 Second table
#' @param column column to consider
#' @export
diff_column <- function(table_1, table_2, column) {
this_time <-
table_2 %>%
dplyr::filter(dplyr::if_any(1, ~ . %in% table_1[[1]])) %>%
dplyr::pull(table_2[column] %>% colnames())
last_time <-
table_1 %>%
dplyr::filter(dplyr::if_any(1, ~ . %in% table_2[[1]])) %>%
dplyr::pull(table_1[column] %>% colnames())
last_time - this_time
}
#' Take the Difference of All Matching Columns in Two Tables
#'
#' This function returns a table of differences for any matching numeric columns
#' in two dataframes. It will only consider rows and columns present in both data
#' sets (row selection is based on the first column).
#' @param table_1 First table
#' @param table_2 Second table
#' @export
diff_tables <- function(table_1, table_2){
is_col_numeric <- table_1 %>% purrr::map_lgl(is.numeric)
numeric_cols <- colnames(table_1)[is_col_numeric]
other_cols <- colnames(table_1)[!is_col_numeric]
diffs <- purrr::map(numeric_cols, ~ sssstats::diff_column(
dplyr::ungroup(table_1), dplyr::ungroup(table_2), .))
filtered_table <- table_1 %>%
dplyr::filter(dplyr::if_any(1, ~ . %in% table_2[[1]]))
stats::setNames(diffs, numeric_cols) %>%
dplyr::as_tibble() %>%
dplyr::bind_cols(filtered_table[other_cols]) %>%
dplyr::relocate(tidyselect::all_of(other_cols),
.before = tidyselect::everything())
}