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

Dan*_*ian 5 r data.table

让我们:

DT1 <- data.table(iris)
DT2 <- DT1 # both reference the same memory location though
DT3 <- copy(DT1)
Run Code Online (Sandbox Code Playgroud)

问题:有没有办法检查是否DT2继续引用相同的内存位置DT1

像这样的伪函数:

mem.identical(DT2, DT1) # should return TRUE
mem.identical(DT3, DT1) # should return FALSE
Run Code Online (Sandbox Code Playgroud)

不幸的是,identical或者all.equal不为此目的而工作,因为

identical(DT1,DT3) # gives TRUE
Run Code Online (Sandbox Code Playgroud)

只有在引入一些变化后,才能使用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
Run Code Online (Sandbox Code Playgroud)

GSe*_*See 6

你可以用data.table::address

> address(DT1)
[1] "0x10336c230"
> address(DT2)
[1] "0x10336c230"
> address(DT3)
[1] "0x10336cb50"
Run Code Online (Sandbox Code Playgroud)

  • (+1),但你应该提到这是`data.table`库函数.. (2认同)