2

Some data:

library(data.table)
dt <- data.table(a = c(1, 2), b = c(3,4))

dt
#    a b
# 1: 1 3
# 2: 2 4

Assign column names to a variable:

cn <- colnames(dt)
cn
# [1] "a" "b"

Add a new column "c" by reference:

dt[ , c := 3]

The vector of original column names 'cn' is also updated and now includes the third variable "c":

cn
# [1] "a" "b" "c"

Are the colnames linked by reference? Can someone can explain it?

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Please, consider adding more information about your problem. Posting only some pieces of code is not a goof practice – Sergey Mell Feb 13 '19 at 10:58
  • Sergey, thank you for your attention ... ones need " to keep your ears open" with data.table! p.s. Thanks for the advice on the design - I will consider later. Good luck! – AndrewTkach Feb 15 '19 at 09:37

0 Answers0