让我们:
DT1 <- data.table(iris)
DT2 <- DT1 # both reference the same memory location though
DT3 <- copy(DT1)
问题:有没有办法检查DT2
是否一直引用与DT1
相同的内存位置?
像这样的伪函数:
mem.identical(DT2, DT1) # should return TRUE
mem.identical(DT3, DT1) # should return FALSE
不幸的是,identical
或all.equal
不能为此目的而工作,因为
identical(DT1,DT3) # gives TRUE
只有在引入一些更改后,才能使用identical
检测差异:
DT1[,Test:=1] # introduces change to DT1 directly, to DT2 indirectly
identical(DT1,DT2) # TRUE - proves that DT2 is linked with DT1
identical(DT1,DT3) # FALSE - DT1 and DT3 are clearly decoupled
答案 0 :(得分:6)
您可以将data.table::address
用于此
> address(DT1)
[1] "0x10336c230"
> address(DT2)
[1] "0x10336c230"
> address(DT3)
[1] "0x10336cb50"
答案 1 :(得分:1)
好的,我使用tracemem
找到了答案on SO here:
DT1 <- data.table(iris)
DT2 <- DT1
DT3 <- copy(DT1)
identical(tracemem(DT1),tracemem(DT2)) # TRUE
identical(tracemem(DT1),tracemem(DT3)) # FALSE