Copy list data.tables

I have the following situation:

1) list of data tables

2) For testing purposes, I intentionally want to (deeply) copy the entire list, including data tables

3) I want to take some element from the copied list and add a new column.

Here is the code:

library(data.table)
x = data.table(aaa = c(1,2))
y = data.table(bbb = c(1,2))
z = list(x,y)
zz = copy(z)

v = zz[[1]]
v = v[, newColumn := 1]

Now I get the following error:

Error in `[.data.table`(res, , `:=`(xxx, TRUE)) : 
(converted from warning) Invalid .internal.selfref detected and fixed
by taking a copy of the whole 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 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.

I don’t understand exactly how copy calls are handled by R and how they are passed to data.table, but not like that: (?)

If someone explicitly uses the copy function, then he / she knows that there is a difference between “by value” and “by reference”. Therefore, he / she must be transferred with a genuine copy of the object.

Therefore, I am of the opinion that there should be no error, and I consider it a “mistake” that the error occurs, nonetheless. It is right?

Fw

+4
1

copy() data.table. list. ..

zz <- lapply(z,copy)
zz[[1]][ , newColumn := 1 ]

, , copy() list data.table. - :

library(data.table)
x = data.table(aaa = c(1,2))
y = data.table(bbb = c(1,2))
z = list(x,y)
zz = copy(z)

#  Both zz$x and z$x are the same object:
.Internal(inspect(zz$x))
#  @7fd58a079778 00 NILSXP g1c0 [MARK,NAM(2)] 
.Internal(inspect(z$x))
#  @7fd58a079778 00 NILSXP g1c0 [MARK,NAM(2)] 
+5

All Articles