0

Sometimes when I work with R and I want to make a copy of a variable to keep it as is, and use it later to monitor changes, R does not copy the variable and all changes are applied to both of them.

Why does this happen? How to prevent it? What part of following image is wrong?

ad

But not all the time:

enter image description here

OverFlow Police
  • 861
  • 6
  • 23

1 Answers1

1

The issue is with copying 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>"
akrun
  • 874,273
  • 37
  • 540
  • 662