summarise_all: Summarise and mutate multiple columns.

Description Usage Arguments Value See Also Examples

View source: R/colwise-mutate.R

Description

These verbs are scoped variants of summarise(), mutate() and transmute(). They apply operations on a selection of variables.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 explicit splicing.

.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(), or a character vector of column names, or a numeric vector of column positions.

.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

vars(), funs()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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"))

Example output

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

# A tibble: 3 x 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
# A tibble: 1 x 2
   height     mass
    <dbl>    <dbl>
1 174.358 97.31186
# A tibble: 1 x 2
   height     mass
    <dbl>    <dbl>
1 174.358 97.31186
# A tibble: 1 x 3
   height     mass birth_year
    <dbl>    <dbl>      <dbl>
1 174.358 97.31186   87.56512
# 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.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 x 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
# A tibble: 3 x 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>
# A tibble: 150 x 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
# A tibble: 150 x 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 x 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 x 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.3
# A tibble: 3 x 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>

dplyr documentation built on Sept. 29, 2017, 1:03 a.m.