Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need an easier way to use dynamically determined symbols #2589

Closed
renkun-ken opened this issue Jan 26, 2018 · 4 comments
Closed

Need an easier way to use dynamically determined symbols #2589

renkun-ken opened this issue Jan 26, 2018 · 4 comments
Labels
duplicate programming parameterizing queries: get, mget, eval, env

Comments

@renkun-ken
Copy link
Member

renkun-ken commented Jan 26, 2018

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.

@tdeenes
Copy link
Member

tdeenes commented Jan 26, 2018

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.

@franknarf1
Copy link
Contributor

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

@jangorecki
Copy link
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

@jangorecki
Copy link
Member

jangorecki commented Mar 22, 2020

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

@jangorecki jangorecki added the programming parameterizing queries: get, mget, eval, env label Apr 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate programming parameterizing queries: get, mget, eval, env
Projects
None yet
Development

No branches or pull requests

4 participants