I'm having a bit of trouble understanding why data table updates columns of a different data table.
Please consider the following reproducible code.
library(data.table)
dt <- data.table(a=rep(letters[1:4], 5),
b=rep(letters[5:8], 5),
c=rep(letters[3:6], 5),
x=sample(1:100, 20),
y=sample(1:100, 20),
z=sample(1:100, 20))
Suppose I assign dt to dt.1:
dt.1 <- dt
Next, suppose I update by reference a column in dt.1:
dt.1[, x:= x^2]
The column x, indeed is squared, but the column x in dt is also squared, i.e.
dt[,x :=x^2]
is performed in the background.
Why does this happen and how can I prevent this type of updating/dependency from happening?
Thanks