Skip to content

Need an easier way to use dynamically determined symbols #2589

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.

Activity

  1. tdeenes commented on Jan 26, 2018

    @tdeenes
    Member

    eval(as.name(s1)) is more efficient. But still, it would be great to generalize the .. notation so that it could replace the eval(as.name()) workaround.

  2. franknarf1 commented on Jan 26, 2018

    @franknarf1
    Contributor

    I think the .. prefix would make sense, maybe a dupe of #633

  3. jangorecki commented on Jan 27, 2018

    @jangorecki
    Member

    AFAIK using language objects is most efficient way, and kind of base R way of handling the task. Two examples:
    https://stackoverflow.com/a/37408321
    https://stackoverflow.com/a/37008966

  4. jangorecki commented on Mar 22, 2020

    @jangorecki
    Member

    to be addressed by #4304

    dt[, s1 * s2, env=list(s1=s1,s2=s2), verbose=TRUE]
    #Argument 'j'  after substitute: x2 * x3
    #Detected that j uses these columns: x2,x3 
    # [1] 10 18 24 28 30 30 28 24 18 10

    closing as duplicate of #2655

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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