With mutate()
you can do 3 things:
Add new columns
Modify existing columns
Delete columns
mutate(
.df,
...,
.by = NULL,
.keep = c("all", "used", "unused", "none"),
.before = NULL,
.after = NULL
)
A data.frame or data.table
Columns to add/modify
Columns to group by
experimental:
This is an experimental argument that allows you to control which columns
from .df
are retained in the output:
"all"
, the default, retains all variables.
"used"
keeps any variables used to make new variables; it's useful
for checking your work as it displays inputs and outputs side-by-side.
"unused"
keeps only existing variables not used to make new
variables.
"none"
, only keeps grouping keys (like transmute()
).
Optionally indicate where new columns should be placed. Defaults to the right side of the data frame.
df <- data.table(
a = 1:3,
b = 4:6,
c = c("a", "a", "b")
)
df %>%
mutate(double_a = a * 2,
a_plus_b = a + b)
#> # A tidytable: 3 × 5
#> a b c double_a a_plus_b
#> <int> <int> <chr> <dbl> <int>
#> 1 1 4 a 2 5
#> 2 2 5 a 4 7
#> 3 3 6 b 6 9
df %>%
mutate(double_a = a * 2,
avg_a = mean(a),
.by = c)
#> # A tidytable: 3 × 5
#> a b c double_a avg_a
#> <int> <int> <chr> <dbl> <dbl>
#> 1 1 4 a 2 1.5
#> 2 2 5 a 4 1.5
#> 3 3 6 b 6 3
df %>%
mutate(double_a = a * 2, .keep = "used")
#> # A tidytable: 3 × 2
#> a double_a
#> <int> <dbl>
#> 1 1 2
#> 2 2 4
#> 3 3 6
df %>%
mutate(double_a = a * 2, .after = a)
#> # A tidytable: 3 × 4
#> a double_a b c
#> <int> <dbl> <int> <chr>
#> 1 1 2 4 a
#> 2 2 4 5 a
#> 3 3 6 6 b