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)#> # A tibble: 3 × 5 #> Species Sepal.Length Sepal.Width Petal.Length Petal.Width #> <fctr> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.006 3.428 1.462 0.246 #> 2 versicolor 5.936 2.770 4.260 1.326 #> 3 virginica 6.588 2.974 5.552 2.026# 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)#> # A tibble: 1 × 2 #> height mass #> <dbl> <dbl> #> 1 174.358 97.31186starwars %>% summarise_at(c("height", "mass"), mean, na.rm = TRUE)#> # A tibble: 1 × 2 #> height mass #> <dbl> <dbl> #> 1 174.358 97.31186starwars %>% summarise_if(is.numeric, mean, na.rm = TRUE)#> # A tibble: 1 × 3 #> height mass birth_year #> <dbl> <dbl> <dbl> #> 1 174.358 97.31186 87.56512# 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 × 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.0 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.0 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.0 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#> # A tibble: 150 × 5 #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <int> <int> <int> <int> <fctr> #> 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 × 9 #> Species Sepal.Length_min Sepal.Width_min Petal.Length_min Petal.Width_min #> <fctr> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 4.3 2.3 1.0 0.1 #> 2 versicolor 4.9 2.0 3.0 1.0 #> 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 × 5 #> # Groups: Species [3] #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <fctr> #> 1 2.007874 1.377953 0.5511811 0.07874016 setosa #> 2 1.929134 1.181102 0.5511811 0.07874016 setosa #> 3 1.850394 1.259843 0.5118110 0.07874016 setosa #> 4 1.811024 1.220472 0.5905512 0.07874016 setosa #> 5 1.968504 1.417323 0.5511811 0.07874016 setosa #> 6 2.125984 1.535433 0.6692913 0.15748031 setosa #> 7 1.811024 1.338583 0.5511811 0.11811024 setosa #> 8 1.968504 1.338583 0.5905512 0.07874016 setosa #> 9 1.732283 1.141732 0.5511811 0.07874016 setosa #> 10 1.929134 1.220472 0.5905512 0.03937008 setosa #> # ... with 140 more rows# Function names will be included if .funs has names or multiple inputs by_species %>% mutate_all(funs(cm = . / 2.54))#> # A tibble: 150 × 9 #> # Groups: Species [3] #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_cm #> <dbl> <dbl> <dbl> <dbl> <fctr> <dbl> #> 1 5.1 3.5 1.4 0.2 setosa 2.007874 #> 2 4.9 3.0 1.4 0.2 setosa 1.929134 #> 3 4.7 3.2 1.3 0.2 setosa 1.850394 #> 4 4.6 3.1 1.5 0.2 setosa 1.811024 #> 5 5.0 3.6 1.4 0.2 setosa 1.968504 #> 6 5.4 3.9 1.7 0.4 setosa 2.125984 #> 7 4.6 3.4 1.4 0.3 setosa 1.811024 #> 8 5.0 3.4 1.5 0.2 setosa 1.968504 #> 9 4.4 2.9 1.4 0.2 setosa 1.732283 #> 10 4.9 3.1 1.5 0.1 setosa 1.929134 #> # ... with 140 more rows, and 3 more variables: Sepal.Width_cm <dbl>, #> # Petal.Length_cm <dbl>, Petal.Width_cm <dbl>#> # A tibble: 3 × 5 #> Species Sepal.Length_med Sepal.Width_med Petal.Length_med Petal.Width_med #> <fctr> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.0 3.4 1.50 0.2 #> 2 versicolor 5.9 2.8 4.35 1.3 #> 3 virginica 6.5 3.0 5.55 2.0#> # A tibble: 3 × 5 #> Species Sepal.Length_Q3 Sepal.Width_Q3 Petal.Length_Q3 Petal.Width_Q3 #> <fctr> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.2 3.675 1.575 0.3 #> 2 versicolor 6.3 3.000 4.600 1.5 #> 3 virginica 6.9 3.175 5.875 2.3by_species %>% summarise_all(c("min", "max"))#> # A tibble: 3 × 9 #> Species Sepal.Length_min Sepal.Width_min Petal.Length_min Petal.Width_min #> <fctr> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 4.3 2.3 1.0 0.1 #> 2 versicolor 4.9 2.0 3.0 1.0 #> 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>