The issue is with copy
ing of the objects. If you don't make a copy
, then it would change at the same memory location. To avoid that, we can copy
the object and this would prevent it from any changes to get updated while the second object changes
library(data.table)
b <- copy(a)
A reproducible example would be
a <- as.data.table(mtcars)
b <- a
b[, v1 := 2]
identical(a, b) # a also changed
#[1] TRUE
If we do copy
a <- as.data.table(mtcars)
b <- copy(a)
b[, v1 := 2]
identical(a, b)
#[1] FALSE
Also, according to ?copy
A copy() may be required when doing dt_names = names(DT). Due to R's copy-on-modify, dt_names still points to the same location in memory as names(DT). Therefore modifying DT by reference now, say by adding a new column, dt_names will also get updated. To avoid this, one has to explicitly copy: dt_names <- copy(names(DT)).
If we do the assignment with <-
, the memory location changes after the change
a <- 2
b <- a
tracemem(a)
#[1] "<0x7fcedcafc370>"
tracemem(b)
#[1] "<0x7fcedcafc370>"
a <- a + 2
tracemem(a) # change here
#[1] "<0x7fcedcaa3ba0>"