0

I have a function that generates a data table, like this:

requiere(dplyr)
requiere(data.table)
f <- function() data.table(A = sample(1:3))

The next step is to apply this function several times and then row bind all the data tables:

dts <- lapply(1:20, function(x) f()) %>% bind_rows

Now, if I try to create a new variable in dts, I get a warning message:

> dts[, new := 1]
Warning message:
In `[.data.table`(dts, , `:=`(new, 1)) :
  Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or been created manually using structure() or similar). Avoid key<-, names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. Also, in R<=v3.0.2, list(DT1,DT2) copied the entire DT1 and DT2 (R's list() used to copy named objects); please upgrade to R>v3.0.2 if that is biting. If this message doesn't help, please report to datatable-help so the root cause can be fixed.

How can I avoid this warning?

mat
  • 2,412
  • 5
  • 31
  • 69
  • Probably something related to `dplyr`. Use `%>% rbindlist` instead of `bind_rows`. Also, your code is very inefficient, just do `dts <- data.table(A = sample(3, 60, replace = TRUE))` instead this whole thing. Although the distribution of A won't be exactly uniform. – David Arenburg Mar 22 '17 at 09:28
  • @DavidArenburg Thank you for your comment. The code is just an example, the actual function is much more complicated. Also, I get the same warning with `rbindlist`. – mat Mar 22 '17 at 09:48
  • check this for the warning http://stackoverflow.com/questions/20687235/warning-invalid-internal-selfref-detected-when-adding-a-column-to-a-data-tab/20688045#20688045 – s.brunel Mar 22 '17 at 09:51
  • Don't know, `dts <- lapply(1:20, function(x) f()) %>% rbindlist ; dts[, new := 1]` works fine for me. See if you have the latest `data.table` verison – David Arenburg Mar 22 '17 at 10:46
  • @DavidArenburg I update data.table and it works now! post your answer and I'll aprove it. Thank you. – mat Mar 22 '17 at 11:55

0 Answers0