I have an original data table dt1
that contains column x
. I want to create another data table called dt2
that contains x
and the first lag of x
. When I execute the following code, I obtain dt2
as desired, but I find that dt1
has also become dt2
--I don't want this to happen.
library(data.table);
x <- rnorm(100, 0, 1);
dt1 <- data.table(x);
dt2 <- dt1;
dt2[, lx:= shift(x, 1, type= "lag")];
identical(dt1, dt2); # evaluates to TRUE
Am I missing something fundamental about how data table works? Any help will be appreciated.