#' Serial shrinking
#'
#' Sequentially applies `group_by` and `summarize` to a data frame.
#'
#' @param data A data frame
#' @param variables Variables to summarize
#' @param grouping A list of single, or vectors of, grouping variables to be used sequentially. The groups are summarized in the order they are provided.
#' @param how A list of summarizing functions to be used, respectively, on each of the grouped data frames defined in `groups`
#'
#' @return A shrunk data frame
#'
#' @export
shrink <- function(data, variables, grouping, how = last) {
if (length(grouping) != length(how)) stop("`grouping` and `how`` must have the same length")
if (length(how) == 1) how <- list(how)
if (!is.list(grouping) & length(grouping) == 1) grouping <- list(grouping)
for (i in seq_along(grouping)) {
data <- data %>%
dplyr::group_by_at(do.call("c", grouping)) %>%
dplyr::summarize_at(variables, how[i])
grouping <- grouping[-1]
}
return (data)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.