Rdatatable / Rdatatable/data.table

[R-Forge #2619] Scoping for LHS in `:=`

Open
#581 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Submitted by: michael neslon; Assigned to: Nobody; R-Forge link

In trying to work a reshape to wide format procedure using data.table and :=

I've stumbled as the LHS of

DT[, c(a) :=b]

does not evaluate in the same scope as the rest of j (including the RHS)
and it doesn't recognize data.table constants like .N or .BY

For example

DT <- data.table(a = letters[1:4], b = letters[1:2], d = 1:8)

DTw <- DT[,.N,by=list(a,b)][, ab := paste0(a,b)]

I'd like to be reshape this to wide using something like

DTw[, c(ab) := N, by = ab]

or

DTw[, .BY := N, by = ab]

both of which will search the global environment for ab and .BY

Currently I can do something like

setkey(DT, ab)

for(nn in DTw[['ab']]){
pp <- DTw[list(nn), list(N)]
DTw[list(nn), c(nn) := pp[['N']]]
}

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.

Research direction

Start with the DT[, c(a) := b] and DTw[, c(ab) := N, by = ab] examples in the issue, then trace how := evaluates its left-hand side versus j and the right-hand side. Compare the desired column and .BY scoping with the current global-environment lookup; done means the proposed reshape expressions resolve those names in the intended data.table scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.