-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate-local.R
33 lines (27 loc) · 951 Bytes
/
aggregate-local.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
# choices for aggregation functions
agg_function_choices <- c("mean", "min", "max")
# given a data frame, return the names of numeric columns
cols_number <- function(df) {
df_select <- dplyr::select(df, where(~is.numeric(.x) | is.integer(.x)) )
names(df_select)
}
# given a data frame, return the names of string and factor columns
cols_category <- function(df) {
df_select <- dplyr::select(df, where(~is.character(.x) | is.factor(.x)) )
names(df_select)
}
# make the aggregation
group_aggregate <- function(df, str_group, str_agg, str_fn_agg,
str_fn_choices = agg_function_choices) {
# validate the aggregation function
stopifnot(
str_fn_agg %in% str_fn_choices
)
# get the aggregation function
func <- get(str_fn_agg)
df |>
dplyr::group_by(dplyr::across(dplyr::all_of(str_group))) |>
dplyr::summarise(
dplyr::across(dplyr::all_of(str_agg), func, na.rm = TRUE)
)
}