测试两个`data.table`是否指向相同的内存位置

时间:2014-05-10 18:48:34

标签: r data.table

让我们:

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

不幸的是,identicalall.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

2 个答案:

答案 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