I have a dataframe where I'd like to apply a different aggregate types (mean, max, min, etc.) to selected columns. How can i modify the code below to allow me to specify different aggregations for fields x3 and x4?
x1 <- c("T", "T", "T", "T", "T", "T", "F", "F", "F", "N")
x2 <- c("A", "B", "A", "B", "A", "B", "A", "B", "C", "D")
x3 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
x4 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
x5 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
d <- data.frame(x1, x2, x3, x4, x5)
# Define grouping columns and aggregated fields
groups <- c("x1", "x2")
fields <- c("x3", "x4")
# Define aggregation functions
types <- list(list(mean = mean,
max = max,
min = min),
list(length = length)
)
# Use summarise() to aggregate data
d %>%
group_by(across(all_of(groups))) %>%
summarise(across(all_of(fields), types))