mutate_all {dplyr} | R Documentation

## Mutate multiple columns

### Description

The scoped variants of `mutate()` and `transmute()` make it easy to apply the same transformation to multiple variables. 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:

### Usage

    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` | A function `fun`, a quosure style lambda `~ fun(.)` or a list of either form.
`...` | Additional arguments for the function calls in `.funs`. These are evaluated only once, with tidy dots support.
`.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()`, a character vector of column names, a numeric vector of column positions, or `NULL`.
`.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).

### Grouping variables

If applied on a grouped tibble, these operations are not applied to the grouping variables. The behaviour depends on whether the selection is implicit (`all` and `if` selections) or explicit (`at` selections).

- Grouping variables covered by explicit selections in `mutate_at()` and `transmute_at()` are always an error. Add `-group_cols()` to the `vars()` selection to avoid this:

      data %>% mutate_at(vars(-group_cols(), ...), myoperation)

  Or remove `group_vars()` from the character vector of column names:

      nms <- setdiff(nms, group_vars(data))
      data %>% mutate_at(vars, myoperation)

- Grouping variables covered by implicit selections are ignored by `mutate_all()`, `transmute_all()`, `mutate_if()`, and `transmute_if()`.

### Naming

The names of the created columns is derived from the names of the input variables and the names of the functions.

- if there is only one unnamed function, the names of the input variables are used to name the created columns

- if there is only one unnamed variable, the names of the functions are used to name the created columns.

- otherwise in the most general case, the created names are created by concatenating the names of the input variables and the names of the functions.

The names of the functions here means the names of the list of functions that is supplied. When needed and not supplied, the name of a function is the prefix "fn" followed by the index of this function within the unnamed functions in the list. Ultimately, names are made unique.

### See Also

The other scoped verbs, `vars()`

### Examples

    iris <- as_tibble(iris)

    # All variants can be passed functions and additional arguments,
    # purrr-style. The _at() variants directly support strings. Here
    # we'll scale the variables `height` and `mass`:
    scale2 <- function(x, na.rm = FALSE) (x - mean(x, na.rm = na.rm)) / sd(x, na.rm)
    starwars %>% mutate_at(c("height", "mass"), scale2)

    # You can pass additional arguments to the function:
    starwars %>% mutate_at(c("height", "mass"), scale2, na.rm = TRUE)

    # You can also pass formulas to create functions on the spot, purrr-style:
    starwars %>% mutate_at(c("height", "mass"), ~scale2(., na.rm = TRUE))

    # You can also supply selection helpers to _at() functions but you have
    # to quote them with vars():
    iris %>% mutate_at(vars(matches("Sepal")), log)

    # The _if() variants apply a predicate function (a function that
    # returns TRUE or FALSE) to determine the relevant subset of
    # columns. Here we divide all the numeric columns by 100:
    starwars %>% mutate_if(is.numeric, scale2, na.rm = TRUE)

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

    # Multiple transformations ----------------------------------------

    # If you want to apply multiple transformations, pass a list of
    # functions. When there are multiple functions, they create new
    # variables instead of modifying the variables in place:
    iris %>% mutate_if(is.numeric, list(scale2, log))

    # The list can contain purrr-style formulas:
    iris %>% mutate_if(is.numeric, list(~scale2(.), ~log(.)))

    # Note how the new variables include the function name, in order to
    # keep things distinct. The default names are not always helpful
    # but you can also supply explicit names:
    iris %>% mutate_if(is.numeric, list(scale = scale2, log = log))

    # When there's only one function in the list, it modifies existing
    # variables in place. Give it a name to instead create new variables:
    iris %>% mutate_if(is.numeric, list(scale2))
    iris %>% mutate_if(is.numeric, list(scale = scale2))

[Package dplyr version 0.8.0.1 Index]