Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Created September 28, 2017 23:03
Show Gist options
  • Save romainfrancois/5efa71010eb1373b656842ba17f1263e to your computer and use it in GitHub Desktop.
Save romainfrancois/5efa71010eb1373b656842ba17f1263e to your computer and use it in GitHub Desktop.
---
title: "Summarise and mutate multiple columns"
output: html_document
---
```{r, include = FALSE}
library(dplyr)
knitr::opts_hooks$set(
asciicast = rsciinema::asciicast_hook
)
```
Examples from `?summarise_all` :
```{r, asciicast=TRUE}
# 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:
starwars %>% summarise_at(vars(height:mass), mean, na.rm = TRUE)
starwars %>% summarise_at(c("height", "mass"), mean, na.rm = TRUE)
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(cm = . / 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"))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment