Is there a way to prevent data.table to print the new data.table after assigning a new column by reference? I gather standard behaviour is
library(data.table)
example(data.table)
DT
# x y v
# 1: a 1 42
# 2: a 3 42
# 3: a 6 42
# 4: b 1 11
# 5: b 3 11
# 6: b 6 11
# 7: c 1 7
# 8: c 3 8
# 9: c 6 9
DT[,z:=1:nrow(DT)]
# x y v z
# 1: a 1 42 1
# 2: a 3 42 2
# 3: a 6 42 3
# 4: b 1 11 4
# 5: b 3 11 5
# 6: b 6 11 6
# 7: c 1 7 7
# 8: c 3 8 8
# 9: c 6 9 9
i.e. the table is printed to screen after assignment. is there a way to stop data.table from showing the new table after assigning the new column z? I know I can stop this behaviour by saying
DT <- copy(DT[,z:=1:nrow(DT)])
but that is defeating the purpose of := (which is designed to avoid copies).
Since
<-.data.tabledoesn’t make a copy, you can use<-:Create a data.table object:
Create a new column:
It is also worth remembering that R only prints the value of an object in interactive mode.
So, in batch mode, you can simply use:
This will not produce any output when run as a script in batch mode.
Further info from Matthew Dowle:
Also see FAQ 2.21 and 2.22 :
Second update from Matthew Dowle:
We have now found a solution and v1.8.3 no longer prints the result when
:=is used. We will update FAQ 2.21 and 2.22.