Skip to content

Need an easier way to use dynamically determined symbols #2589

@renkun-ken

Description

@renkun-ken

Suppose I have a data.table created as follows:

dt <- data.table(x1 = 1:10, x2 = 10:1, x3 = 1:10)

I need to do calculations with dynamically determined symbols within j like

s1 <- "x2"
s2 <- "x3"

Two approaches can do the work:

> dt[, get(s1) * get(s2)]
 [1] 10 18 24 28 30 30 28 24 18 10
> dt[, .SD[[1]] * .SD[[2]], .SDcols = c(s1, s2)]
 [1] 10 18 24 28 30 30 28 24 18 10

But if the data is very big and by= is used, the performance can significantly decay. Also the first approach using get() has scoping problem if s1 or s2 are themselves columns of dt.

Is there any possibility that makes it easier to use dynamically determined symbol without such significant performance decay and scoping problem?

For example, something like

dt[, ..s1 * ..s2]

which is inspired by the ..x notation introduced lately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateprogrammingparameterizing queries: get, mget, eval, env

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions