复制data.tables列表

时间:2015-06-22 10:30:55

标签: r data.table

我有以下情况:

1)数据表列表

2)出于测试目的,我故意想要(深度)复制整个列表,包括数据表

3)我想从复制的列表中取一些元素并添加一个新列。

以下是代码:

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]

现在我收到以下错误:

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'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.

我不确切地理解R如何处理复制调用以及如何将它们传递给data.table但是不是这样:(?)

如果某人明确使用了复制功能,那么他/她就会意识到“按值”和“按引用”之间存在差异。所以他/她应该分发出对象的真实副本。

因此,我认为应该没有任何错误,我认为它仍然是错误发生的“错误”。这是对的吗?

FW

1 个答案:

答案 0 :(得分:5)

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)]