EVOLUTION-MANAGER
Edit File: mutate_all.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Mutate multiple columns</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for mutate_all {dplyr}"><tr><td>mutate_all {dplyr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Mutate multiple columns</h2> <h3>Description</h3> <a href='https://www.tidyverse.org/lifecycle/#superseded'><img src='figures/lifecycle-superseded.svg' alt='Superseded lifecycle'></a> <p>Scoped verbs (<code style="white-space: pre;">_if</code>, <code style="white-space: pre;">_at</code>, <code style="white-space: pre;">_all</code>) have been superseded by the use of <code><a href="across.html">across()</a></code> in an existing verb. See <code>vignette("colwise")</code> for details. </p> <p>The <a href="scoped.html">scoped</a> variants of <code><a href="mutate.html">mutate()</a></code> and <code><a href="mutate.html">transmute()</a></code> make it easy to apply the same transformation to multiple variables. There are three variants: </p> <ul> <li><p> _all affects every variable </p> </li> <li><p> _at affects variables selected with a character vector or vars() </p> </li> <li><p> _if affects variables selected with a predicate function: </p> </li></ul> <h3>Usage</h3> <pre> 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) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>.tbl</code></td> <td> <p>A <code>tbl</code> object.</p> </td></tr> <tr valign="top"><td><code>.funs</code></td> <td> <p>A function <code>fun</code>, a quosure style lambda <code>~ fun(.)</code> or a list of either form.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Additional arguments for the function calls in <code>.funs</code>. These are evaluated only once, with <a href="../../rlang/html/dyn-dots.html">tidy dots</a> support.</p> </td></tr> <tr valign="top"><td><code>.predicate</code></td> <td> <p>A predicate function to be applied to the columns or a logical vector. The variables for which <code>.predicate</code> is or returns <code>TRUE</code> are selected. This argument is passed to <code><a href="../../rlang/html/as_function.html">rlang::as_function()</a></code> and thus supports quosure-style lambda functions and strings representing function names.</p> </td></tr> <tr valign="top"><td><code>.vars</code></td> <td> <p>A list of columns generated by <code><a href="vars.html">vars()</a></code>, a character vector of column names, a numeric vector of column positions, or <code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>.cols</code></td> <td> <p>This argument has been renamed to <code>.vars</code> to fit dplyr's terminology and is deprecated.</p> </td></tr> </table> <h3>Value</h3> <p>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). </p> <h3>Grouping variables</h3> <p>If applied on a grouped tibble, these operations are <em>not</em> applied to the grouping variables. The behaviour depends on whether the selection is <strong>implicit</strong> (<code>all</code> and <code>if</code> selections) or <strong>explicit</strong> (<code>at</code> selections). </p> <ul> <li><p> Grouping variables covered by explicit selections in <code>mutate_at()</code> and <code>transmute_at()</code> are always an error. Add <code>-group_cols()</code> to the <code><a href="vars.html">vars()</a></code> selection to avoid this:</p> <pre>data %>% mutate_at(vars(-group_cols(), ...), myoperation) </pre> <p>Or remove <code>group_vars()</code> from the character vector of column names:</p> <pre>nms <- setdiff(nms, group_vars(data)) data %>% mutate_at(vars, myoperation) </pre> </li> <li><p> Grouping variables covered by implicit selections are ignored by <code>mutate_all()</code>, <code>transmute_all()</code>, <code>mutate_if()</code>, and <code>transmute_if()</code>. </p> </li></ul> <h3>Naming</h3> <p>The names of the new columns are derived from the names of the input variables and the names of the functions. </p> <ul> <li><p> if there is only one unnamed function (i.e. if <code>.funs</code> is an unnamed list of length one), the names of the input variables are used to name the new columns; </p> </li> <li><p> for <code style="white-space: pre;">_at</code> functions, if there is only one unnamed variable (i.e., if <code>.vars</code> is of the form <code>vars(a_single_column)</code>) and <code>.funs</code> has length greater than one, the names of the functions are used to name the new columns; </p> </li> <li><p> otherwise, the new names are created by concatenating the names of the input variables and the names of the functions, separated with an underscore <code>"_"</code>. </p> </li></ul> <p>The <code>.funs</code> 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, <code><a href="vars.html">vars()</a></code> accepts named and unnamed arguments. If a variable in <code>.vars</code> is named, a new column by that name will be created. </p> <p>Name collisions in the new columns are disambiguated using a unique suffix. </p> <h3>Life cycle</h3> <p>The functions are maturing, because the naming scheme and the disambiguation algorithm are subject to change in dplyr 0.9.0. </p> <h3>See Also</h3> <p><a href="scoped.html">The other scoped verbs</a>, <code><a href="vars.html">vars()</a></code> </p> <h3>Examples</h3> <pre> 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 another iris %>% 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)) </pre> <hr /><div style="text-align: center;">[Package <em>dplyr</em> version 1.0.2 <a href="00Index.html">Index</a>]</div> </body></html>