EVOLUTION-MANAGER
Edit File: summarise_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: Summarise 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 summarise_all {dplyr}"><tr><td>summarise_all {dplyr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Summarise 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="summarise.html">summarise()</a></code> make it easy to apply the same transformation to multiple variables. There are three variants. </p> <ul> <li> <p><code>summarise_all()</code> affects every variable </p> </li> <li> <p><code>summarise_at()</code> affects variables selected with a character vector or vars() </p> </li> <li> <p><code>summarise_if()</code> affects variables selected with a predicate function </p> </li></ul> <h3>Usage</h3> <pre> 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) </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>summarise_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 %>% summarise_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 %>% summarise_at(nms, myoperation) </pre> </li> <li><p> Grouping variables covered by implicit selections are silently ignored by <code>summarise_all()</code> and <code>summarise_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> # The _at() variants directly support strings: starwars %>% summarise_at(c("height", "mass"), mean, na.rm = TRUE) # -> starwars %>% summarise(across(c("height", "mass"), ~ mean(.x, na.rm = TRUE))) # You can also supply selection helpers to _at() functions but you have # to quote them with vars(): starwars %>% summarise_at(vars(height:mass), mean, na.rm = TRUE) # -> starwars %>% summarise(across(height:mass, ~ mean(.x, na.rm = TRUE))) # The _if() variants apply a predicate function (a function that # returns TRUE or FALSE) to determine the relevant subset of # columns. Here we apply mean() to the numeric columns: starwars %>% summarise_if(is.numeric, mean, na.rm = TRUE) starwars %>% summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE))) by_species <- iris %>% group_by(Species) # 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: by_species %>% summarise_all(list(min, max)) # -> by_species %>% summarise(across(everything(), list(min = min, max = max))) </pre> <hr /><div style="text-align: center;">[Package <em>dplyr</em> version 1.0.2 <a href="00Index.html">Index</a>]</div> </body></html>