复制 data.tables 的列表
copy a list of data.tables
我有以下情况:
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 但它不是这样的吗:(?)
如果有人明确使用复制功能,那么 he/she 就会知道 'by-value' 和 'by-reference' 之间存在差异。所以he/she应该交出对象的真实副本。
因此,我认为不应该有任何错误,我认为'bug'错误仍然发生。对吗?
转发
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)]
我有以下情况:
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 但它不是这样的吗:(?)
如果有人明确使用复制功能,那么 he/she 就会知道 'by-value' 和 'by-reference' 之间存在差异。所以he/she应该交出对象的真实副本。
因此,我认为不应该有任何错误,我认为'bug'错误仍然发生。对吗?
转发
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)]