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

Add DT[a==1][, b:=1] vs DT[a==1, b:=1] to ?":=" #905

Closed
matthieugomez opened this issue Oct 22, 2014 · 4 comments
Closed

Add DT[a==1][, b:=1] vs DT[a==1, b:=1] to ?":=" #905

matthieugomez opened this issue Oct 22, 2014 · 4 comments
Assignees
Milestone

Comments

@matthieugomez
Copy link
Contributor

I could not find anything in the documentation about how

DT <- data.table (a = 1)
DT[a==1][, b:=1]

is actually different from

DT <- data.table (a = 1)
DT1 <- DT[a==1]
DT1[,  b := 1]

Maybe the distinction should be written somewhere. This behavior surprised me.

@mattdowle
Copy link
Member

They are the same in fact, iiuc. DT[a==1] returns a new data.table. In the first example the compound [,b:=1] is applied to that new data.table and the result hasn't been assigned to a symbol so that's lost. DT1 <- DT[a==1][, b:=1] was probably intended. And that's identical to the the DT1 that the 2nd example creates.

Agreed on documentation. Maybe it should state that := assigns by reference to whatever object is "passed" immediately to the left of the square bracket. Grateful for any suggestions of what exactly to write and where. (I find that when I ask, the response it often different to what I would have added!) I just had a look and ?":=" seems to be lacking on compound examples.

@mattdowle mattdowle added this to the v1.9.8 milestone Oct 22, 2014
@mattdowle mattdowle changed the title DT[a==1][, b:=1] Add DT[a==1][, b:=1] vs DT[a==1, b:=1] to ?":=" Oct 22, 2014
@matthieugomez
Copy link
Contributor Author

Sorry - your explanation is clear, now I don't know even why I thought the two should give the same result.

@mattdowle
Copy link
Member

No worries. Still, a good addition to ?":=". Will do.

@arunsrinivasan
Copy link
Member

Added as Note to ?":=".

@arunsrinivasan arunsrinivasan self-assigned this Mar 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants