[datatable-help] Is assignment such as DT[, a:=7] supposed to print DT when surrounded by braces?
Todd A. Johnson
tjohnson at src.riken.jp
Wed Mar 12 11:59:43 CET 2014
I am using data.table Version 1.9.2 with R 3.0.2 on Mac OS 10.6.8.
I've looked through 6 months worth of the mailing list as well as the Bug
reports and of course the FAQ vignette. However, while my question seems
related to FAQ 2.21, that answer seems to say that returning DT when
assigning DT[i,col:=value] was made invisible in v1.8.3.
My question comes from observing different behavior for assignment by
reference to a column when a data.table DT is surrounded by braces compared
to without braces (such as within an if..else statement).
Here's a simple test program:
library(data.table)
DT <- data.table(a=c(1,2,3), b=c(4,5,6))
DT[,d:=7]
DT <- data.table(a=c(1,2,3), b=c(4,5,6))
if( nrow(DT)>0 ){DT[,d:=7]}
On my computer, it does the following:
> DT <- data.table(a=c(1,2,3), b=c(4,5,6))
> DT[,d:=7]
> DT <- data.table(a=c(1,2,3), b=c(4,5,6))
> if( nrow(DT)>0 ){DT[,d:=7]}
a b d
1: 1 4 7
2: 2 5 7
3: 3 6 7
So, should the second assignment within the 'if' statement print out DT? To
get rid of this effect in my scripts (which potentially could result in
printing out tens-of-thousands of rows of data into a log file...), I
surrounded my last assignment to DT with invisible(), but it seems strange
to me that that would be necessary.
Thank you!
Todd
More information about the datatable-help
mailing list