I have trouble wrapping my head arount the following example :
If I works with simple numbers, e.g.
library(dplyr)
x=5
y <- x %>% +1
y
x
x==y
I get what I had in mind, that is x is not changed
> y
[1] 6
> x
[1] 5
> x==y
[1] FALSE
Now it I do that with datatables :
library(data.table)
DT = data.table(id=c("A","B","C"),Value=c(1,2,3))
DT2 <- DT %>% .[,DoubleValue:=2*Value]
DT
DT2
DT==DT2
I get something I see as conceptually different :
> DT
id Value DoubleValue
1: A 1 2
2: B 2 4
3: C 3 6
> DT2
id Value DoubleValue
1: A 1 2
2: B 2 4
3: C 3 6
> DT==DT2
id Value DoubleValue
[1,] TRUE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] TRUE TRUE TRUE
That is that DT is actually changed
Most examples I have found actually do not use the <- part and are happy to modify their object anyway.
Why is DT changed but not x ?