summarise_all {dplyr} | R Documentation

## Summarise and mutate multiple columns.

### Description

These verbs are scoped variants of `summarise()`, `mutate()` and `transmute()`. They apply operations on a selection of variables.

- `summarise_all()`, `mutate_all()` and `transmute_all()` apply the functions to all (non-grouping) columns.

- `summarise_at()`, `mutate_at()` and `transmute_at()` allow you to select columns using the same name-based select_helpers just like with `select()`.

- `summarise_if`(), `mutate_if`() and `transmute_if()` operate on columns for which a predicate returns `TRUE`.

### Usage

    summarise_all(.tbl, .funs, ...)

    summarise_if(.tbl, .predicate, .funs, ...)

    summarise_at(.tbl, .vars, .funs, ..., .cols = NULL)

    summarize_all(.tbl, .funs, ...)

    summarize_if(.tbl, .predicate, .funs, ...)

    summarize_at(.tbl, .vars, .funs, ..., .cols = NULL)

    mutate_all(.tbl, .funs, ...)

    mutate_if(.tbl, .predicate, .funs, ...)

    mutate_at(.tbl, .vars, .funs, ..., .cols = NULL)

    transmute_all(.tbl, .funs, ...)

    transmute_if(.tbl, .predicate, .funs, ...)

    transmute_at(.tbl, .vars, .funs, ..., .cols = NULL)

### Arguments

`.tbl` | A `tbl` object.
`.funs` | List of function calls generated by `funs()`, or a character vector of function names, or simply a function. Bare formulas are passed to `rlang::as_function()` to create purrr-style lambda functions. Note that these lambda prevent hybrid evaluation from happening and it is thus more efficient to supply functions like `mean()` directly rather than in a lambda-formula.
`...` | Additional arguments for the function calls in `.funs`. These are evaluated only once, with explicit splicing.
`.predicate` | A predicate function to be applied to the columns or a logical vector. The variables for which `.predicate` is or returns `TRUE` are selected. This argument is passed to `rlang::as_function()` and thus supports quosure-style lambda functions and strings representing function names.
`.vars` | A list of columns generated by `vars()`, or a character vector of column names, or a numeric vector of column positions.
`.cols` | This argument has been renamed to `.vars` to fit dplyr's terminology and is deprecated.

### Value

A data frame. By default, the newly created columns have the shortest names needed to uniquely identify the output. To force inclusion of a name, even when not needed, name the input (see examples for details).

### See Also

`vars()`, `funs()`

### Examples

    # The scoped variants of summarise() and mutate() make it easy to
    # apply the same transformation to multiple variables:

    iris %>%
      group_by(Species) %>%
      summarise_all(mean)

    # There are three variants.
    # * _all affects every variable
    # * _at affects variables selected with a character vector or vars()
    # * _if affects variables selected with a predicate function:

    starwars %>% summarise_at(vars(height:mass), mean, na.rm = TRUE)
    starwars %>% summarise_at(c("height", "mass"), mean, na.rm = TRUE)
    starwars %>% summarise_if(is.numeric, mean, na.rm = TRUE)

    # mutate_if is particularly useful for transforming variables from
    # one type to another
    iris %>% as_tibble() %>% mutate_if(is.factor, as.character)
    iris %>% as_tibble() %>% mutate_if(is.double, as.integer)

    # ---------------------------------------------------------------------------
    # If you want apply multiple transformations, use funs()
    by_species <- iris %>% group_by(Species)

    by_species %>% summarise_all(funs(min, max))
    # Note that output variable name now includes the function name, in order to
    # keep things distinct.

    # You can express more complex inline transformations using .
    by_species %>% mutate_all(funs(. / 2.54))

    # Function names will be included if .funs has names or multiple inputs
    by_species %>% mutate_all(funs(cm = . / 2.54))
    by_species %>% summarise_all(funs(med = median))
    by_species %>% summarise_all(funs(Q3 = quantile), probs = 0.75)
    by_species %>% summarise_all(c("min", "max"))

[Package dplyr version 0.7.0 Index]