Add new variables to data frame
mutate.RdAdds new variables to existing data frame using a vector of character expressions.
Usage
mutate(.data, ..., .frame = NULL, .env = parent.frame())
# S3 method for class 'data.frame'
mutate(.data, ..., .frame = NULL, .env = parent.frame())
# S3 method for class 'group_df'
mutate(.data, ..., .frame = NULL, .env = parent.frame())
# S3 method for class 'nest_df'
mutate(.data, ..., .frame = NULL, .env = parent.frame(), .list_column = TRUE)
mutate_(data, x, frame = NULL, env = parent.frame(), ...)
# S3 method for class 'data.frame'
mutate_(data, x, frame = NULL, env = parent.frame(), ...)
# S3 method for class 'group_df'
mutate_(data, x, frame = NULL, env = parent.frame(), ...)
# S3 method for class 'nest_df'
mutate_(data, x, frame = NULL, env = parent.frame(), list_column = TRUE, ...)Arguments
- .data, data
A data frame.
- ...
Expressions to be evaluated.
- .frame, frame
A character vector of column names referred to in
x. This subsets the data frame to only specified columns before evaluation. This provides safer evaluation by preventing accidental use of spurious global variables in place of column names or accidental use of spurious column names in place of global variables.- .env, env
An environment to look for objects outside those in
data.- .list_column, list_column
TRUEorFALSE. If mutating on anest_df, thenTRUEwill evaluate and add the new variable(s) inside the list-column.FALSEwill evaluate and add the new variable(s) outside the list-column.- x
A named character vector (or named list of character vectors) of expressions.
Examples
#----------------------------------------------------------------------------
# mutate() examples
#----------------------------------------------------------------------------
library(bkdat)
df <- data.frame(a = c(1, 2, 3), b = c(4, 5, 6))
mutate(df, c = -1*a, d = log(b))
#> a b c d
#> 1 1 4 -1 1.386294
#> 2 2 5 -2 1.609438
#> 3 3 6 -3 1.791759
mutate_(df, c(c = "-1*a", d = "log(b)"))
#> a b c d
#> 1 1 4 -1 1.386294
#> 2 2 5 -2 1.609438
#> 3 3 6 -3 1.791759