R/slice_max_helper.R

Defines functions slice_max_helper

Documented in slice_max_helper

#' select row with maximum value of a variable for each grouping variable
#'
#' @param df a data frame
#' @param group_var a grouping variable
#' @param max_var a numeric variable
#' @param n number of elements to select from each group
#' @examples
#' library(dplyr)
#' mtcars %>% slice_max_helper(vs, mpg)
#'
#' @export
slice_max_helper <- function(df, group_var, max_var, n = 1) {
  df %>%
    dplyr::group_by({{group_var}}) %>%
    dplyr::slice_max({{max_var}}, with_ties = F, n = n) %>%
    dplyr::ungroup()
}
PatrickCoyle/cmisc documentation built on June 23, 2022, 3:07 p.m.