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)

Arguments

.tbl

A tbl object.

.funs

List of function calls generated by funs(), or a character vector of function names, or simply a function.

Bare formulas are passed to rlang::as_function() to create purrr-style lambda functions. Note that these lambda prevent hybrid evaluation from happening and it is thus more efficient to supply functions like mean() directly rather than in a lambda-formula.

...

Additional arguments for the function calls in .funs. These are evaluated only once, with tidy dots support.

.predicate

A predicate function to be applied to the columns or a logical vector. The variables for which .predicate is or returns TRUE are selected. This argument is passed to rlang::as_function() and thus supports quosure-style lambda functions and strings representing function names.

.vars

A list of columns generated by vars(), a character vector of column names, a numeric vector of column positions, or NULL.

.cols

This argument has been renamed to .vars to fit dplyr's terminology and is deprecated.

Value

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).

See also

Examples

# 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)
#> # A tibble: 3 x 5 #> Species Sepal.Length Sepal.Width Petal.Length Petal.Width #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.01 3.43 1.46 0.246 #> 2 versicolor 5.94 2.77 4.26 1.33 #> 3 virginica 6.59 2.97 5.55 2.03
# 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)
#> # A tibble: 1 x 2 #> height mass #> <dbl> <dbl> #> 1 174. 97.3
# You can also supply selection helpers to _at() functions but you have # to quote them with vars(): iris %>% mutate_at(vars(matches("Sepal")), log)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1 1.629241 1.2527630 1.4 0.2 setosa #> 2 1.589235 1.0986123 1.4 0.2 setosa #> 3 1.547563 1.1631508 1.3 0.2 setosa #> 4 1.526056 1.1314021 1.5 0.2 setosa #> 5 1.609438 1.2809338 1.4 0.2 setosa #> 6 1.686399 1.3609766 1.7 0.4 setosa #> 7 1.526056 1.2237754 1.4 0.3 setosa #> 8 1.609438 1.2237754 1.5 0.2 setosa #> 9 1.481605 1.0647107 1.4 0.2 setosa #> 10 1.589235 1.1314021 1.5 0.1 setosa #> 11 1.686399 1.3083328 1.5 0.2 setosa #> 12 1.568616 1.2237754 1.6 0.2 setosa #> 13 1.568616 1.0986123 1.4 0.1 setosa #> 14 1.458615 1.0986123 1.1 0.1 setosa #> 15 1.757858 1.3862944 1.2 0.2 setosa #> 16 1.740466 1.4816045 1.5 0.4 setosa #> 17 1.686399 1.3609766 1.3 0.4 setosa #> 18 1.629241 1.2527630 1.4 0.3 setosa #> 19 1.740466 1.3350011 1.7 0.3 setosa #> 20 1.629241 1.3350011 1.5 0.3 setosa #> 21 1.686399 1.2237754 1.7 0.2 setosa #> 22 1.629241 1.3083328 1.5 0.4 setosa #> 23 1.526056 1.2809338 1.0 0.2 setosa #> 24 1.629241 1.1939225 1.7 0.5 setosa #> 25 1.568616 1.2237754 1.9 0.2 setosa #> 26 1.609438 1.0986123 1.6 0.2 setosa #> 27 1.609438 1.2237754 1.6 0.4 setosa #> 28 1.648659 1.2527630 1.5 0.2 setosa #> 29 1.648659 1.2237754 1.4 0.2 setosa #> 30 1.547563 1.1631508 1.6 0.2 setosa #> 31 1.568616 1.1314021 1.6 0.2 setosa #> 32 1.686399 1.2237754 1.5 0.4 setosa #> 33 1.648659 1.4109870 1.5 0.1 setosa #> 34 1.704748 1.4350845 1.4 0.2 setosa #> 35 1.589235 1.1314021 1.5 0.2 setosa #> 36 1.609438 1.1631508 1.2 0.2 setosa #> 37 1.704748 1.2527630 1.3 0.2 setosa #> 38 1.589235 1.2809338 1.4 0.1 setosa #> 39 1.481605 1.0986123 1.3 0.2 setosa #> 40 1.629241 1.2237754 1.5 0.2 setosa #> 41 1.609438 1.2527630 1.3 0.3 setosa #> 42 1.504077 0.8329091 1.3 0.3 setosa #> 43 1.481605 1.1631508 1.3 0.2 setosa #> 44 1.609438 1.2527630 1.6 0.6 setosa #> 45 1.629241 1.3350011 1.9 0.4 setosa #> 46 1.568616 1.0986123 1.4 0.3 setosa #> 47 1.629241 1.3350011 1.6 0.2 setosa #> 48 1.526056 1.1631508 1.4 0.2 setosa #> 49 1.667707 1.3083328 1.5 0.2 setosa #> 50 1.609438 1.1939225 1.4 0.2 setosa #> 51 1.945910 1.1631508 4.7 1.4 versicolor #> 52 1.856298 1.1631508 4.5 1.5 versicolor #> 53 1.931521 1.1314021 4.9 1.5 versicolor #> 54 1.704748 0.8329091 4.0 1.3 versicolor #> 55 1.871802 1.0296194 4.6 1.5 versicolor #> 56 1.740466 1.0296194 4.5 1.3 versicolor #> 57 1.840550 1.1939225 4.7 1.6 versicolor #> 58 1.589235 0.8754687 3.3 1.0 versicolor #> 59 1.887070 1.0647107 4.6 1.3 versicolor #> 60 1.648659 0.9932518 3.9 1.4 versicolor #> 61 1.609438 0.6931472 3.5 1.0 versicolor #> 62 1.774952 1.0986123 4.2 1.5 versicolor #> 63 1.791759 0.7884574 4.0 1.0 versicolor #> 64 1.808289 1.0647107 4.7 1.4 versicolor #> 65 1.722767 1.0647107 3.6 1.3 versicolor #> 66 1.902108 1.1314021 4.4 1.4 versicolor #> 67 1.722767 1.0986123 4.5 1.5 versicolor #> 68 1.757858 0.9932518 4.1 1.0 versicolor #> 69 1.824549 0.7884574 4.5 1.5 versicolor #> 70 1.722767 0.9162907 3.9 1.1 versicolor #> 71 1.774952 1.1631508 4.8 1.8 versicolor #> 72 1.808289 1.0296194 4.0 1.3 versicolor #> 73 1.840550 0.9162907 4.9 1.5 versicolor #> 74 1.808289 1.0296194 4.7 1.2 versicolor #> 75 1.856298 1.0647107 4.3 1.3 versicolor #> 76 1.887070 1.0986123 4.4 1.4 versicolor #> 77 1.916923 1.0296194 4.8 1.4 versicolor #> 78 1.902108 1.0986123 5.0 1.7 versicolor #> 79 1.791759 1.0647107 4.5 1.5 versicolor #> 80 1.740466 0.9555114 3.5 1.0 versicolor #> 81 1.704748 0.8754687 3.8 1.1 versicolor #> 82 1.704748 0.8754687 3.7 1.0 versicolor #> 83 1.757858 0.9932518 3.9 1.2 versicolor #> 84 1.791759 0.9932518 5.1 1.6 versicolor #> 85 1.686399 1.0986123 4.5 1.5 versicolor #> 86 1.791759 1.2237754 4.5 1.6 versicolor #> 87 1.902108 1.1314021 4.7 1.5 versicolor #> 88 1.840550 0.8329091 4.4 1.3 versicolor #> 89 1.722767 1.0986123 4.1 1.3 versicolor #> 90 1.704748 0.9162907 4.0 1.3 versicolor #> 91 1.704748 0.9555114 4.4 1.2 versicolor #> 92 1.808289 1.0986123 4.6 1.4 versicolor #> 93 1.757858 0.9555114 4.0 1.2 versicolor #> 94 1.609438 0.8329091 3.3 1.0 versicolor #> 95 1.722767 0.9932518 4.2 1.3 versicolor #> 96 1.740466 1.0986123 4.2 1.2 versicolor #> 97 1.740466 1.0647107 4.2 1.3 versicolor #> 98 1.824549 1.0647107 4.3 1.3 versicolor #> 99 1.629241 0.9162907 3.0 1.1 versicolor #> 100 1.740466 1.0296194 4.1 1.3 versicolor #> 101 1.840550 1.1939225 6.0 2.5 virginica #> 102 1.757858 0.9932518 5.1 1.9 virginica #> 103 1.960095 1.0986123 5.9 2.1 virginica #> 104 1.840550 1.0647107 5.6 1.8 virginica #> 105 1.871802 1.0986123 5.8 2.2 virginica #> 106 2.028148 1.0986123 6.6 2.1 virginica #> 107 1.589235 0.9162907 4.5 1.7 virginica #> 108 1.987874 1.0647107 6.3 1.8 virginica #> 109 1.902108 0.9162907 5.8 1.8 virginica #> 110 1.974081 1.2809338 6.1 2.5 virginica #> 111 1.871802 1.1631508 5.1 2.0 virginica #> 112 1.856298 0.9932518 5.3 1.9 virginica #> 113 1.916923 1.0986123 5.5 2.1 virginica #> 114 1.740466 0.9162907 5.0 2.0 virginica #> 115 1.757858 1.0296194 5.1 2.4 virginica #> 116 1.856298 1.1631508 5.3 2.3 virginica #> 117 1.871802 1.0986123 5.5 1.8 virginica #> 118 2.041220 1.3350011 6.7 2.2 virginica #> 119 2.041220 0.9555114 6.9 2.3 virginica #> 120 1.791759 0.7884574 5.0 1.5 virginica #> 121 1.931521 1.1631508 5.7 2.3 virginica #> 122 1.722767 1.0296194 4.9 2.0 virginica #> 123 2.041220 1.0296194 6.7 2.0 virginica #> 124 1.840550 0.9932518 4.9 1.8 virginica #> 125 1.902108 1.1939225 5.7 2.1 virginica #> 126 1.974081 1.1631508 6.0 1.8 virginica #> 127 1.824549 1.0296194 4.8 1.8 virginica #> 128 1.808289 1.0986123 4.9 1.8 virginica #> 129 1.856298 1.0296194 5.6 2.1 virginica #> 130 1.974081 1.0986123 5.8 1.6 virginica #> 131 2.001480 1.0296194 6.1 1.9 virginica #> 132 2.066863 1.3350011 6.4 2.0 virginica #> 133 1.856298 1.0296194 5.6 2.2 virginica #> 134 1.840550 1.0296194 5.1 1.5 virginica #> 135 1.808289 0.9555114 5.6 1.4 virginica #> 136 2.041220 1.0986123 6.1 2.3 virginica #> 137 1.840550 1.2237754 5.6 2.4 virginica #> 138 1.856298 1.1314021 5.5 1.8 virginica #> 139 1.791759 1.0986123 4.8 1.8 virginica #> 140 1.931521 1.1314021 5.4 2.1 virginica #> 141 1.902108 1.1314021 5.6 2.4 virginica #> 142 1.931521 1.1314021 5.1 2.3 virginica #> 143 1.757858 0.9932518 5.1 1.9 virginica #> 144 1.916923 1.1631508 5.9 2.3 virginica #> 145 1.902108 1.1939225 5.7 2.5 virginica #> 146 1.902108 1.0986123 5.2 2.3 virginica #> 147 1.840550 0.9162907 5.0 1.9 virginica #> 148 1.871802 1.0986123 5.2 2.0 virginica #> 149 1.824549 1.2237754 5.4 2.3 virginica #> 150 1.774952 1.0986123 5.1 1.8 virginica
starwars %>% summarise_at(vars(height:mass), mean, na.rm = TRUE)
#> # A tibble: 1 x 2 #> height mass #> <dbl> <dbl> #> 1 174. 97.3
# 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)
#> # A tibble: 1 x 3 #> height mass birth_year #> <dbl> <dbl> <dbl> #> 1 174. 97.3 87.6
# mutate_if() is particularly useful for transforming variables from # one type to another iris %>% as_tibble() %>% mutate_if(is.factor, as.character)
#> # A tibble: 150 x 5 #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <chr> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa #> 7 4.6 3.4 1.4 0.3 setosa #> 8 5 3.4 1.5 0.2 setosa #> 9 4.4 2.9 1.4 0.2 setosa #> 10 4.9 3.1 1.5 0.1 setosa #> # ... with 140 more rows
iris %>% as_tibble() %>% mutate_if(is.double, as.integer)
#> # A tibble: 150 x 5 #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <int> <int> <int> <int> <fct> #> 1 5 3 1 0 setosa #> 2 4 3 1 0 setosa #> 3 4 3 1 0 setosa #> 4 4 3 1 0 setosa #> 5 5 3 1 0 setosa #> 6 5 3 1 0 setosa #> 7 4 3 1 0 setosa #> 8 5 3 1 0 setosa #> 9 4 2 1 0 setosa #> 10 4 3 1 0 setosa #> # ... with 140 more rows
# --------------------------------------------------------------------------- # If you want apply multiple transformations, use funs() by_species <- iris %>% group_by(Species) by_species %>% summarise_all(funs(min, max))
#> # A tibble: 3 x 9 #> Species Sepal.Length_min Sepal.Width_min Petal.Length_min Petal.Width_min #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 4.3 2.3 1 0.1 #> 2 versicolor 4.9 2 3 1 #> 3 virginica 4.9 2.2 4.5 1.4 #> # ... with 4 more variables: Sepal.Length_max <dbl>, Sepal.Width_max <dbl>, #> # Petal.Length_max <dbl>, Petal.Width_max <dbl>
# 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))
#> # A tibble: 150 x 5 #> # Groups: Species [3] #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <fct> #> 1 2.01 1.38 0.551 0.0787 setosa #> 2 1.93 1.18 0.551 0.0787 setosa #> 3 1.85 1.26 0.512 0.0787 setosa #> 4 1.81 1.22 0.591 0.0787 setosa #> 5 1.97 1.42 0.551 0.0787 setosa #> 6 2.13 1.54 0.669 0.157 setosa #> 7 1.81 1.34 0.551 0.118 setosa #> 8 1.97 1.34 0.591 0.0787 setosa #> 9 1.73 1.14 0.551 0.0787 setosa #> 10 1.93 1.22 0.591 0.0394 setosa #> # ... with 140 more rows
# Function names will be included if .funs has names or multiple inputs by_species %>% mutate_all(funs(inches = . / 2.54))
#> # A tibble: 150 x 9 #> # Groups: Species [3] #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_inch… #> <dbl> <dbl> <dbl> <dbl> <fct> <dbl> #> 1 5.1 3.5 1.4 0.2 setosa 2.01 #> 2 4.9 3 1.4 0.2 setosa 1.93 #> 3 4.7 3.2 1.3 0.2 setosa 1.85 #> 4 4.6 3.1 1.5 0.2 setosa 1.81 #> 5 5 3.6 1.4 0.2 setosa 1.97 #> 6 5.4 3.9 1.7 0.4 setosa 2.13 #> 7 4.6 3.4 1.4 0.3 setosa 1.81 #> 8 5 3.4 1.5 0.2 setosa 1.97 #> 9 4.4 2.9 1.4 0.2 setosa 1.73 #> 10 4.9 3.1 1.5 0.1 setosa 1.93 #> # ... with 140 more rows, and 3 more variables: Sepal.Width_inches <dbl>, #> # Petal.Length_inches <dbl>, Petal.Width_inches <dbl>
by_species %>% summarise_all(funs(med = median))
#> # A tibble: 3 x 5 #> Species Sepal.Length_med Sepal.Width_med Petal.Length_med Petal.Width_med #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5 3.4 1.5 0.2 #> 2 versicolor 5.9 2.8 4.35 1.3 #> 3 virginica 6.5 3 5.55 2
by_species %>% summarise_all(funs(Q3 = quantile), probs = 0.75)
#> # A tibble: 3 x 5 #> Species Sepal.Length_Q3 Sepal.Width_Q3 Petal.Length_Q3 Petal.Width_Q3 #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.2 3.68 1.58 0.3 #> 2 versicolor 6.3 3 4.6 1.5 #> 3 virginica 6.9 3.18 5.88 2.3
by_species %>% summarise_all(c("min", "max"))
#> # A tibble: 3 x 9 #> Species Sepal.Length_min Sepal.Width_min Petal.Length_min Petal.Width_min #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 4.3 2.3 1 0.1 #> 2 versicolor 4.9 2 3 1 #> 3 virginica 4.9 2.2 4.5 1.4 #> # ... with 4 more variables: Sepal.Length_max <dbl>, Sepal.Width_max <dbl>, #> # Petal.Length_max <dbl>, Petal.Width_max <dbl>