mutate | R Documentation |
mutate()
adds new variables and preserves existing ones; transmute()
adds new variables and drops existing ones.
Both functions preserve the number of rows of the input. New variables overwrite existing variables of the same name.
Variables can be removed by setting their value to NULL
.
mutate(.data, ...)
## S3 method for class 'data.frame'
mutate(
.data,
...,
.keep = c("all", "used", "unused", "none"),
.before = NULL,
.after = NULL
)
transmute(.data, ...)
.data |
A |
... |
Name-value pairs of expressions, each with length |
.keep |
This argument allows you to control which columns from
Grouping variables are always kept, unconditional to |
.before, .after |
< |
+
, -
, log()
, etc., for their usual mathematical meanings
lead()
, lag()
dense_rank()
, min_rank()
, percent_rank()
, row_number()
, cume_dist()
, ntile()
cumsum()
, cummin()
, cummax()
na_if()
, coalesce()
if_else()
, recode()
, case_when()
mutate(mtcars, mpg2 = mpg * 2)
mtcars %>% mutate(mpg2 = mpg * 2)
mtcars %>% mutate(mpg2 = mpg * 2, cyl2 = cyl * 2)
# Newly created variables are available immediately
mtcars %>% mutate(mpg2 = mpg * 2, mpg4 = mpg2 * 2)
# You can also use mutate() to remove variables and modify existing variables
mtcars %>% mutate(
mpg = NULL,
disp = disp * 0.0163871 # convert to litres
)
# By default, new columns are placed on the far right.
# You can override this with `.before` or `.after`.
df <- data.frame(x = 1, y = 2)
df %>% mutate(z = x + y)
df %>% mutate(z = x + y, .before = 1)
df %>% mutate(z = x + y, .after = x)
# By default, mutate() keeps all columns from the input data.
# You can override with `.keep`
df <- data.frame(
x = 1, y = 2, a = "a", b = "b",
stringsAsFactors = FALSE
)
df %>% mutate(z = x + y, .keep = "all") # the default
df %>% mutate(z = x + y, .keep = "used")
df %>% mutate(z = x + y, .keep = "unused")
df %>% mutate(z = x + y, .keep = "none") # same as transmute()
# mutate() vs transmute --------------------------
# mutate() keeps all existing variables
mtcars %>%
mutate(displ_l = disp / 61.0237)
# transmute keeps only the variables you create
mtcars %>%
transmute(displ_l = disp / 61.0237)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.