相关疑难解决方法(0)

准确理解data.table何时是对另一个data.table的引用(与副本)

我在理解传递的引用属性方面遇到了一些麻烦data.table.有些操作似乎"打破"了参考,我想准确理解发生了什么.

data.table从另一个创建a 时data.table(通过<-,然后更新新表:=,原始表也会被更改.这是预期的,按照:

?data.table::copystackoverflow:传递引用操作在数据表包中

这是一个例子:

library(data.table)

DT <- data.table(a=c(1,2), b=c(11,12))
print(DT)
#      a  b
# [1,] 1 11
# [2,] 2 12

newDT <- DT        # reference, not copy
newDT[1, a := 100] # modify new DT

print(DT)          # DT is modified too.
#        a  b
# [1,] 100 11
# [2,]   2 12
Run Code Online (Sandbox Code Playgroud)

但是,如果我:=<-赋值和:=上面的行之间插入非基础修改,DT现在不再修改:

DT = data.table(a=c(1,2), b=c(11,12))
newDT …
Run Code Online (Sandbox Code Playgroud)

copy r reference assignment-operator data.table

181
推荐指数
2
解决办法
3万
查看次数

标签 统计

assignment-operator ×1

copy ×1

data.table ×1

r ×1

reference ×1