1

I get .internal.selfref warning when executing the below three lines of code in R.

From the warning, I think the second line of the code makes a copy of the data.table without keeping proper references.

Can anyone explain how this occurs mechanically?

For example, replacing the second line with dt[, y:=1] resolves the issue, but I don't understand why.

My R version is 3.1.2.

> dt = data.table(x=1:2)
> dt[["y"]] = 1
> dt[, z:=1]
Warning message:
In `[.data.table`(dt, .`:=`(z, 1)) :
Invalid .internal.selfref detected ...
JK Lim
  • 45
  • 4
  • Yes, it seems that `[[<-.data.table` does not exist, so the function `[[<-.data.frame` is used instead. For whatever reason, the latter makes a copy ... or (more likely?) data.table thinks it makes a copy. It plays nice with `dt$y = 1` (which also defers to a data.frame function, I think) and `dt[,"y"] = 1` (which uses `[.data.table`) – Frank Sep 10 '15 at 16:53
  • Lots of details in this answer http://stackoverflow.com/a/20688045/1191259 Interesting to type `tracemem(dt)` before and after the various ways of adding `y`. Seems that only `:=` modifies by reference, while the others move/copy, even `dt[,"y"] = 1` which only uses data.table functions. – Frank Sep 10 '15 at 17:02

0 Answers0