0

We are facing an abnormal situation where a data.table object seems to be assigned by reference instead of being copied by value. This situation shows up when using the operator ':='

(data.table -> 1.9.6 ; R -> 3.2)

Example:

a <- data.table(matrix(4,4,4))
b <- a

b[,`:=`(V5=6,V4=2)]
print(a)

This would happen also if the assignment is done with '='

Nevertheless, this wouldn't happen if we used b <- copy(a)

Can someone explain what's going on?

Rafa GIG
  • 1
  • 2
  • 2
    `copy()` means deep copy, and you copy the actual data from table `a` to `b` by `b <- copy(a)` so `a` and `b` are different objects now. However `b <- a` or `b = a` only copy the reference so essentially `b` and `a` point to the same table. When you modify `b` by `:=`, you modify `a` at the same time. – Psidom Jun 07 '16 at 15:05
  • 2
    Please read the [data.table vignettes](https://github.com/Rdatatable/data.table/wiki/Getting-started). They explain this. It's not "abnormal", it's a feature and intended behavior. – Roland Jun 07 '16 at 15:10
  • I think the reason it is a little bit confusing is that `data.table` in R is actually mutable objects unlike other R objects from my understanding. – Psidom Jun 07 '16 at 15:13
  • 1
    @Psidom: yes, but that gives huge performance boost when used properly – digEmAll Jun 07 '16 at 15:17

0 Answers0