# NOT RUN {
# 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"))
# }
Run the code above in your browser using DataLab