Like mutate-table.express but possibly recycling calls.

mutate_sd(.data, .SDcols, .how = identity, ...)

# S3 method for ExprBuilder
mutate_sd(
  .data,
  .SDcols,
  .how = identity,
  ...,
  .pairwise = TRUE,
  .prefix,
  .suffix,
  .parse = getOption("table.express.parse", FALSE),
  .chain = getOption("table.express.chain", TRUE)
)

# S3 method for EagerExprBuilder
mutate_sd(.data, ..., .parent_env = rlang::caller_env())

# S3 method for data.table
mutate_sd(.data, ...)

Arguments

.data

An instance of ExprBuilder.

.SDcols

See data.table::data.table and the details here.

.how

The function(s) or function call(s) that will perform the transformation. If many, a list should be used, either with list() or .(). If the list is named, the names will be used for the new columns' names. Lambdas specified as formulas are supported.

...

Possibly more arguments for all functions/calls in .how.

.pairwise

If FALSE, each function in .how is applied to each column in .SDcols (like a cartesian product).

.prefix, .suffix

Only relevant when .how is a function: add a prefix or suffix to the new column's name. If neither is missing, .prefix has preference.

.parse

Logical. Whether to apply rlang::parse_expr() to obtain the expressions.

.chain

Logical. Should a new frame be automatically chained to the expression if the clause being set already exists?

.parent_env

See end_expr()

Details

This function works similar to transmute_sd() but keeps all columns and can modify by reference, like mutate-table.express. It can serve like dplyr's scoped mutation variants depending on what's given to .SDcols.

Additionally, .SDcols supports:

  • tidyselect::select_helpers

  • A predicate using the .COL pronoun that should return a single logical when .COL is replaced by a column of the data.

  • A formula using . or .x instead of the aforementioned .COL.

The caveat is that the expression is evaluated eagerly, i.e. with the currently captured data.table. Consider using chain() to explicitly capture intermediate results as actual data.tables.

To see more examples, check the vignette, or the table.express-package entry.

Examples


data("mtcars")

data.table::as.data.table(mtcars) %>%
    start_expr %>%
    mutate_sd(c("mpg", "cyl"), ~ .x * 2)
#> .DT_[, `:=`(c("mpg", "cyl"), .mutate_matching(.SD, c("mpg", "cyl"
#> ), rlang::quos(~.x * 2)))]