Understanding the reference properties of data.table in R

Just to clear some things for myself, I would like to better understand when copies are made, and when they are not in data.table. Since this question points to Understanding, when data.table is a reference to (vs copy of) another data.table , if you just run the following, then you end up to change the original:

library(data.table)

DT <- data.table(a=c(1,2), b=c(11,12))
print(DT)
#      a  b
# [1,] 1 11
# [2,] 2 12

newDT <- DT        # reference, not copy
newDT[1, a := 100] # modify new DT

print(DT)          # DT is modified too.
#        a  b
# [1,] 100 11
# [2,]   2 12

However, if you do this (for example), you will end up changing the new version:

DT = data.table(a=1:10)
DT
     a
 1:  1
 2:  2
 3:  3
 4:  4
 5:  5
 6:  6
 7:  7
 8:  8
 9:  9
10: 10

newDT = DT[a<11]
newDT
     a
 1:  1
 2:  2
 3:  3
 4:  4
 5:  5
 6:  6
 7:  7
 8:  8
 9:  9
10: 10

newDT[1:5,a:=0L]

newDT
     a
 1:  0
 2:  0
 3:  0
 4:  0
 5:  0
 6:  6
 7:  7
 8:  8
 9:  9
10: 10

DT
     a
 1:  1
 2:  2
 3:  3
 4:  4
 5:  5
 6:  6
 7:  7
 8:  8
 9:  9
10: 10

As I understand it, the reason is that this is because when you execute the statement i, it data.tablereturns the whole new table, and not a link to the memory occupied by the selection elements of the old one data.table. Is this correct and true?

EDIT: , i not j ( )

+5
1

newDT , i (not j). := j. i , , .

A data.table - . == , , (, := j)

data.table, truelength (tl = 100) -

 .Internal(inspect(DT))
@1427d6c8 19 VECSXP g0c7 [OBJ,NAM(2),ATT] (len=1, tl=100)
  @b249a30 13 INTSXP g0c4 [NAM(2)] (len=10, tl=0) 1,2,3,4,5,...

10 tl=0. truelength , .

?truelength

, (.. (DT)), , ()

i, data.table , , ( ), .

+7

All Articles