transmute_sd: Transmute subset of data

View source: R/VERBS-transmute_sd.R

transmute_sdR Documentation

Transmute subset of data

Description

Like transmute-table.express but for a single call and maybe specifying .SDcols.

Usage

transmute_sd(.data, .SDcols = everything(), .how = identity, ...)

## S3 method for class 'ExprBuilder'
transmute_sd(
  .data,
  .SDcols = everything(),
  .how = identity,
  ...,
  .parse = getOption("table.express.parse", FALSE),
  .chain = getOption("table.express.chain", TRUE)
)

## S3 method for class 'EagerExprBuilder'
transmute_sd(.data, ..., .parent_env = rlang::caller_env())

## S3 method for class 'data.table'
transmute_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.

.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

Like transmute-table.express, this function never modifies the input by reference. This function adds/chains a select expression that will be evaluated by data.table::data.table, possibly specifying the helper function .transmute_matching, which is assigned to the final expression's evaluation environment when calling end_expr() (i.e., ExprBuilder's eval method).

Said function supports two pronouns that can be used by .how and .SDcols:

  • .COL: the actual values of the column.

  • .COLNAME: the name of the column currently being evaluated.

Additionally, lambdas specified as formulas are also supported. In those cases, .x is equivalent to .COL and .y to .COLNAME.

Unlike a call like DT[, (vars) := expr], .SDcols can be created dynamically with an expression that evaluates to something that would be used in place of vars without eagerly using the captured data.table. See the examples here or in table.express-package.

Examples


data("mtcars")

data.table::as.data.table(mtcars) %>%
    transmute_sd(~ grepl("^d", .y), ~ .x * 2)

data.table::as.data.table(mtcars) %>%
    transmute_sd(~ is.numeric(.x), ~ .x * 2)


table.express documentation built on April 3, 2023, 5:43 p.m.