6

Let's have:

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

Question: Is there a way to check that DT2 keeps referencing the same memory location as DT1?

Something like this pseudo-function:

mem.identical(DT2, DT1) # should return TRUE
mem.identical(DT3, DT1) # should return FALSE

Unfortunately, identical or all.equal don't work for this purpose, because

identical(DT1,DT3) # gives TRUE

Only after introducing some change, the difference can be detected using 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
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
  • Here's a different question with a duplicate answer: http://stackoverflow.com/a/10913296/403310 (that I only found because it's linked in the documentation for `address`) – GSee May 10 '14 at 19:35
  • @BenBolker actually the value added is `data.table::address` solution by @GSee, which I didn't find elsewhere – Daniel Krizian May 10 '14 at 19:35

2 Answers2

7

You can use data.table::address for this

> address(DT1)
[1] "0x10336c230"
> address(DT2)
[1] "0x10336c230"
> address(DT3)
[1] "0x10336cb50"
GSee
  • 48,880
  • 13
  • 125
  • 145
  • 2
    (+1), but you should probably mention that this is `data.table` library function.. – David Arenburg May 10 '14 at 19:27
  • 1
    @DavidArenburg I didn't think it was necessary because the Question includes `data.table` in the title, it is tagged [data.table] and the first line of code to create the sample data is `DT1 <- data.table(iris)`. ;-) – GSee May 10 '14 at 19:29
  • Hadley also has an `address` function in his **pryr** package (https://github.com/hadley/pryr/blob/master/R/inspect.r) that serves the same purpose, but since `data.table` is already loaded there's no reason to load another package. – GSee May 10 '14 at 19:54
2

Ok I found the answer on SO here, using tracemem:

DT1 <- data.table(iris)
DT2 <- DT1
DT3 <- copy(DT1)

identical(tracemem(DT1),tracemem(DT2)) # TRUE
identical(tracemem(DT1),tracemem(DT3)) # FALSE
Community
  • 1
  • 1
Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
  • 2
    Please, remember you need to call untracemem after call tracemem, otherwise you'll get a message every time DT1, DT2 or DT3 change... – digEmAll May 10 '14 at 19:15
  • Since you found the answer on SO, you should probably vote to close (if you have sufficient rep) *or* comment (if you don't) rather than just copying the answer? (I admit I haven't looked to see whether there's any value-added in your answer over the original ...) – Ben Bolker May 10 '14 at 19:32
  • Ok will do accordingly next time, instead of copying the answer, wasn't sure about proper SO etiquette. Thanks @BenBolker – Daniel Krizian May 10 '14 at 19:45