mutate_all function - RDocumentation (2024)


mutate_all function - RDocumentation (1)

Scoped verbs (_if, _at, _all) have been superseded by the use ofacross() in an existing verb. See vignette("colwise") for details.

The scoped variants of mutate() and transmute() make it easy to applythe 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:


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)


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



A tbl object.


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.


A predicate function to be applied to the columnsor a logical vector. The variables for which .predicate is orreturns TRUE are selected. This argument is passed torlang::as_function() and thus supports quosure-style lambdafunctions and strings representing function names.


A list of columns generated by vars(),a character vector of column names, a numeric vector of columnpositions, or NULL.


This argument has been renamed to .vars to fitdplyr's terminology and is deprecated.

Grouping variables

If applied on a grouped tibble, these operations are not appliedto the grouping variables. The behaviour depends on whether theselection is implicit (all and if selections) orexplicit (at selections).

  • Grouping variables covered by explicit selections inmutate_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 bymutate_all(), transmute_all(), mutate_if(), andtransmute_if().


The names of the new columns are derived from the names of theinput variables and the names of the functions.

  • if there is only one unnamed function (i.e. if .funs is an unnamed listof length one),the names of the input variables are used to name the new columns;

  • for _at functions, if there is only one unnamed variable (i.e.,if .vars is of the form vars(a_single_column)) and .funs has lengthgreater than one,the names of the functions are used to name the new columns;

  • otherwise, the new names are created byconcatenating the names of the input variables and the names of thefunctions, separated with an underscore "_".

The .funs argument can be a named or unnamed list.If a function is unnamed and the name cannot be derived automatically,a name of the form "fn#" is used.Similarly, vars() accepts named and unnamed arguments.If a variable in .vars is named, a new column by that name will be created.

Name collisions in the new columns are disambiguated using a unique suffix.

Life cycle

The functions are maturing, because the naming scheme and thedisambiguation algorithm are subject to change in dplyr 0.9.0.

See Also

The other scoped verbs, vars()


Run this code

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)# ->starwars %>% mutate(across(c("height", "mass"), scale2))# You can pass additional arguments to the function:starwars %>% mutate_at(c("height", "mass"), scale2, na.rm = TRUE)starwars %>% mutate_at(c("height", "mass"), ~scale2(., na.rm = TRUE))# ->starwars %>% mutate(across(c("height", "mass"), ~ scale2(.x, 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)iris %>% mutate(across(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)starwars %>% mutate(across(where(is.numeric), ~ scale2(.x, na.rm = TRUE)))# mutate_if() is particularly useful for transforming variables from# one type to anotheriris %>% mutate_if(is.factor, as.character)iris %>% mutate_if(is.double, as.integer)# ->iris %>% mutate(across(where(is.factor), as.character))iris %>% mutate(across(where(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))iris %>% mutate_if(is.numeric, list(~scale2(.), ~log(.)))iris %>% mutate_if(is.numeric, list(scale = scale2, log = log))# ->iris %>% as_tibble() %>% mutate(across(where(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))

Run the code above in your browser using DataLab

mutate_all function - RDocumentation (2024)


Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6107

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.