1

The code below illustrates my issue:

> A <- data.table(value=1)
> A_cols <- names(A)
> A[,new_value:=2]
> A_cols
[1] "value"     "new_value"

Here the variable A_cols was indirectly modified when the := modifier was used to modify the data.table A. This was unexpected for me.

I understand why this use of := would modify the value pointed to by a data.table assigned via something like B <- A, but that isn't what's happening here. The variable A_cols is the result of applying a function to my data.table.

Also, this behavior appears to be inconsistent since

> A <- data.table(value=1)
> A_num_cols <- ncol(A)
> A[,new_value:=2]
> A_num_cols
[1] 1

leaves the variable A_num_cols unaltered.

I wondered if the code behind names is the culprit, but there is no information in the documentation that would give a clue as to why this behavior should be expected.

Is this indeed expected, and if so, are there any explanations as to the philosophy behind this and why it seems to be applied inconsistently?

Rookatu
  • 1,487
  • 3
  • 21
  • 50
  • 3
    [Why does data.table update names(DT) by reference, even if I assign to another variable?](https://stackoverflow.com/questions/15913417/why-does-data-table-update-namesdt-by-reference-even-if-i-assign-to-another-v) – Henrik Apr 17 '19 at 22:07
  • Hi @Henrik. The link you provided fully explains what is going on here. If you post the link as an answer I can accept, or perhaps the question should be marked as duplicate? Not sure what the protocol is here. Thank you for the information :) – Rookatu Apr 24 '19 at 15:31
  • Hi @Rookatu Thanks for your feedback. If the linked post answers your question, there is no need for me to reiterate the answer here. I just close as a dupe and your question will serve as a signpost for future visitors. Cheers. – Henrik Apr 24 '19 at 15:42

0 Answers0