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

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

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

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

# S3 method for 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)
#>      disp drat
#>  1: 320.0 7.80
#>  2: 320.0 7.80
#>  3: 216.0 7.70
#>  4: 516.0 6.16
#>  5: 720.0 6.30
#>  6: 450.0 5.52
#>  7: 720.0 6.42
#>  8: 293.4 7.38
#>  9: 281.6 7.84
#> 10: 335.2 7.84
#> 11: 335.2 7.84
#> 12: 551.6 6.14
#> 13: 551.6 6.14
#> 14: 551.6 6.14
#> 15: 944.0 5.86
#> 16: 920.0 6.00
#> 17: 880.0 6.46
#> 18: 157.4 8.16
#> 19: 151.4 9.86
#> 20: 142.2 8.44
#> 21: 240.2 7.40
#> 22: 636.0 5.52
#> 23: 608.0 6.30
#> 24: 700.0 7.46
#> 25: 800.0 6.16
#> 26: 158.0 8.16
#> 27: 240.6 8.86
#> 28: 190.2 7.54
#> 29: 702.0 8.44
#> 30: 290.0 7.24
#> 31: 602.0 7.08
#> 32: 242.0 8.22
#>      disp drat

data.table::as.data.table(mtcars) %>%
    transmute_sd(~ is.numeric(.x), ~ .x * 2)
#>      mpg cyl  disp  hp drat     wt  qsec vs am gear carb
#>  1: 42.0  12 320.0 220 7.80  5.240 32.92  0  2    8    8
#>  2: 42.0  12 320.0 220 7.80  5.750 34.04  0  2    8    8
#>  3: 45.6   8 216.0 186 7.70  4.640 37.22  2  2    8    2
#>  4: 42.8  12 516.0 220 6.16  6.430 38.88  2  0    6    2
#>  5: 37.4  16 720.0 350 6.30  6.880 34.04  0  0    6    4
#>  6: 36.2  12 450.0 210 5.52  6.920 40.44  2  0    6    2
#>  7: 28.6  16 720.0 490 6.42  7.140 31.68  0  0    6    8
#>  8: 48.8   8 293.4 124 7.38  6.380 40.00  2  0    8    4
#>  9: 45.6   8 281.6 190 7.84  6.300 45.80  2  0    8    4
#> 10: 38.4  12 335.2 246 7.84  6.880 36.60  2  0    8    8
#> 11: 35.6  12 335.2 246 7.84  6.880 37.80  2  0    8    8
#> 12: 32.8  16 551.6 360 6.14  8.140 34.80  0  0    6    6
#> 13: 34.6  16 551.6 360 6.14  7.460 35.20  0  0    6    6
#> 14: 30.4  16 551.6 360 6.14  7.560 36.00  0  0    6    6
#> 15: 20.8  16 944.0 410 5.86 10.500 35.96  0  0    6    8
#> 16: 20.8  16 920.0 430 6.00 10.848 35.64  0  0    6    8
#> 17: 29.4  16 880.0 460 6.46 10.690 34.84  0  0    6    8
#> 18: 64.8   8 157.4 132 8.16  4.400 38.94  2  2    8    2
#> 19: 60.8   8 151.4 104 9.86  3.230 37.04  2  2    8    4
#> 20: 67.8   8 142.2 130 8.44  3.670 39.80  2  2    8    2
#> 21: 43.0   8 240.2 194 7.40  4.930 40.02  2  0    6    2
#> 22: 31.0  16 636.0 300 5.52  7.040 33.74  0  0    6    4
#> 23: 30.4  16 608.0 300 6.30  6.870 34.60  0  0    6    4
#> 24: 26.6  16 700.0 490 7.46  7.680 30.82  0  0    6    8
#> 25: 38.4  16 800.0 350 6.16  7.690 34.10  0  0    6    4
#> 26: 54.6   8 158.0 132 8.16  3.870 37.80  2  2    8    2
#> 27: 52.0   8 240.6 182 8.86  4.280 33.40  0  2   10    4
#> 28: 60.8   8 190.2 226 7.54  3.026 33.80  2  2   10    4
#> 29: 31.6  16 702.0 528 8.44  6.340 29.00  0  2   10    8
#> 30: 39.4  12 290.0 350 7.24  5.540 31.00  0  2   10   12
#> 31: 30.0  16 602.0 670 7.08  7.140 29.20  0  2   10   16
#> 32: 42.8   8 242.0 218 8.22  5.560 37.20  2  2    8    4
#>      mpg cyl  disp  hp drat     wt  qsec vs am gear carb