`mutate()` adds new variables and preserves existing ones; `transmute()` adds new variables and drops existing ones. New variables overwrite existing variables of the same name. Variables can be removed by setting their value to `NULL`.

Arguments

.data

A tbl. (See dplyr)

...

<[`tidy-eval`][dplyr_tidy_eval]> Name-value pairs. The name gives the name of the column in the output.

The value can be:

* A vector of length 1, which will be recycled to the correct length. * A vector the same length as the current group (or the whole data frame if ungrouped). * `NULL`, to remove the column. * A data frame or tibble, to create multiple columns in the output.

Value

An object of the same type as `.data`.

For `mutate()`:

* Rows are not affected. * Existing columns will be preserved unless explicitly modified. * New columns will be added to the right of existing columns. * Columns given value `NULL` will be removed * Groups will be recomputed if a grouping variable is mutated. * Data frame attributes are preserved.

For `transmute()`:

* Rows are not affected. * Apart from grouping variables, existing columns will be remove unless explicitly kept. * Column order matches order of expressions. * Groups will be recomputed if a grouping variable is mutated. * Data frame attributes are preserved.

Useful mutate functions

* [`+`], [`-`], [log()], etc., for their usual mathematical meanings

* [lead()], [lag()]

* [dense_rank()], [min_rank()], [percent_rank()], [row_number()], [cume_dist()], [ntile()]

* [cumsum()], [cummean()], [cummin()], [cummax()], [cumany()], [cumall()]

* [na_if()], [coalesce()]

* [if_else()], [recode()], [case_when()]

Grouped tibbles

Because mutating expressions are computed within groups, they may yield different results on grouped tibbles. This will be the case as soon as an aggregating, lagging, or ranking function is involved. Compare this ungrouped mutate:

With the grouped equivalent:

The former normalises `mass` by the global average whereas the latter normalises by the averages within gender levels.

Methods

These function are **generic**s, which means that packages can provide implementations (methods) for other classes. See the documentation of individual methods for extra arguments and differences in behaviour.

Methods available in currently loaded packages:

See also

Other single table verbs: arrange(), filter(), rename(), summarise()

Examples


# Newly created variables are available immediately
mtcars |> as_tibble() |> mutate(
  cyl2 = cyl * 2,
  cyl4 = cyl2 * 2
)
#> # A tibble: 32 × 13
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  cyl2  cyl4
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4    12    24
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4    12    24
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1     8    16
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1    12    24
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2    16    32
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1    12    24
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4    16    32
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2     8    16
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2     8    16
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4    12    24
#> # ℹ 22 more rows