R/summarise.R

#' Reduces multiple values down to a single value
#'
#' @param .data   a list.
#' @param \dots   name-value pairs of summary functions. The name will be the name
#'                of the variable in the result.
#' @param gather  before calculating the summaries, each variable is first passed through
#'                the \code{gather} function. Common choices for \code{gather} are
#'                \code{\link{unlist}}, or \code{\link{unlist_with_nas}}. If data is to
#'                be taken as-is, use \code{\link{identity}} function.
#' @param warn    if \code{FALSE} (default) it ignores the warnings throwed by
#'                purrr's \code{\link[purrr]{transpose}}.
#'
#' @examples
#'
#' as_lol(mtcars) %>%
#'   summarise(
#'     avg_mpg = mean(mpg),
#'     max_cyl = max(cyl),
#'     n = length(cyl)
#'   )
#'
#' as_lov(mtcars) %>%
#'   group_by(gear) %>%
#'   summarise(
#'     avg_mpg = mean(mpg),
#'     n = length(cyl)
#'   )
#'
#' @export

summarise.list <- function(.data, ...,
                           gather = unlist_with_nas,
                           warn = FALSE) {
  dots <- quos(...)

  if (!warn)
    transp <- function(x) suppressWarnings(transpose(x))
  else
    transp <- transpose

  t_data <- transp(.data)
  t_data <- lapply(t_data, gather)
  try_eval(dots, data = t_data)
}
twolodzko/lolplyr documentation built on May 14, 2019, 8:22 a.m.