This is a method for the dplyr::mutate()
generic.
mutate()
creates new columns that are functions of existing variables.
It can also modify (if the name is the same as an existing column)
and delete columns (by setting their value to NULL
).
Arguments
- .data
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.
- ...
<
data-masking
> 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.
- .by
-
<
tidy-select
> Optionally, a selection of columns to group by for just this operation, functioning as an alternative togroup_by()
. For details and examples, see ?dplyr_by. - .keep
Control which columns from
.data
are retained in the output. Grouping columns and columns created by...
are always kept."all"
retains all columns from.data
. This is the default."used"
retains only the columns used in...
to create new columns. This is useful for checking your work, as it displays inputs and outputs side-by-side."unused"
retains only the columns not used in...
to create new columns. This is useful if you generate new columns, but no longer need the columns used to generate them."none"
doesn't retain any extra columns from.data
. Only the grouping variables and columns created by...
are kept.
- .before, .after
<
tidy-select
> Optionally, control where new columns should appear (the default is to add to the right hand side). Seerelocate()
for more details.
Examples
library(duckplyr)
df <- data.frame(x = c(1, 2))
df <- mutate(df, y = 2)
df
#> x y
#> 1 1 2
#> 2 2 2