Using `..` in j have limitations in expression, RHS of assignment

Open
#4,554 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reproducing the two examples in the issue, covering expressions with two .. references and the combination of (col2) := ..col2 * 2. Trace how data.table evaluates .. in j, expressions, and assignment, then establish and test the intended behavior or document the supported limitation.

Written by the indexing model from the issue text.

Description

programming

I tried to search if this was reported before, but .. is ignored in github search so I cannot really search this problem.

  • we can use .. in j to access column name saved in variable, but it will fail with an expression involving two .. reference.
dt <- data.table(mtcars)
col1 <- "hp"
col2 <- "cyl"
# this looks like returned the data.table that have col1 removed. 
# I knew there is usage of `-..col1` to remove that column, 
# but this is confusing and make below problematic
dt[, ..col1 - ..col2]
> dt[, ..col1 - ..col2]
     mpg cyl  disp drat    wt  qsec vs am gear carb
 1: 21.0   6 160.0 3.90 2.620 16.46  0  1    4    4
 2: 21.0   6 160.0 3.90 2.875 17.02  0  1    4    4
 3: 22.8   4 108.0 3.85 2.320 18.61  1  1    4    1
# adding () doesn't fix this
dt[, (..col1) - (..col2)]
  • we can use () in LHS of assignment, .. in RHS of assignment, but not together
dt[, (col2) := 8]
dt[, ..col2]
dt[, (col2) := ..col2 * 2]
# Error in ..col2 * 2 : non-numeric argument to binary operator

So is .. supposed to be used only as itself and cannot be used in any expression?

I knew dt[[col1]] will always work, but that will not work when we also are using selective condition in i at the same time.

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.