This seems like very un-desireable behavior. Lets say I create a data.table, a
, with columns x
and y
.
library(data.table)
x <- rnorm(100)
y <- rnorm(100)
a <- data.table(x,y)
I want to perform some calculations on the columns of this data.table, while retaining a "clean" copy of the data. So I make a new data.table object, b
and perform some calculations that generate new columns in data.table b
:
b <- a
b[,new.var := x*y]
This new variable, new.var
is showing up in BOTH data.table objects a
and b
. I never told a
to create this column, it seems to be auto-added from b
. My question is, why is this desirable behavior? Head of each data.table below:
> head(a)
y x new.var
1: 0.1369316 -2.0992072 -0.2874478
2: -0.1074927 -0.8418876 0.0904968
3: -0.8530955 -1.3860180 1.1824057
4: -1.8503559 0.3464916 -0.6411328
5: -0.9671240 -2.7080326 2.6190034
6: 1.8149524 -1.3655645 -2.4784345
> head(b)
y x new.var
1: 0.1369316 -2.0992072 -0.2874478
2: -0.1074927 -0.8418876 0.0904968
3: -0.8530955 -1.3860180 1.1824057
4: -1.8503559 0.3464916 -0.6411328
5: -0.9671240 -2.7080326 2.6190034
6: 1.8149524 -1.3655645 -2.4784345