summarise_all {dplyr} | R Documentation |
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
.
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)
.tbl |
A |
.funs |
List of function calls generated by Bare formulas are passed to |
... |
Additional arguments for the function calls in
|
.predicate |
A predicate function to be applied to the columns
or a logical vector. The variables for which |
.vars |
A list of columns generated by |
.cols |
This argument has been renamed to |
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).
# 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: # The _at() variants directly support strings: starwars %>% summarise_at(c("height", "mass"), mean, 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) starwars %>% summarise_at(vars(height:mass), mean, 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) # 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(inches = . / 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"))