Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, it's subassignment in R using <code>&lt;-</code> (or <code>=</code> or <code>-&gt;</code>) that makes a copy of the <em>whole</em> object. You can trace that using <code>tracemem(DT)</code> and <code>.Internal(inspect(DT))</code>, as below. The <code>data.table</code> features <code>:=</code> and <code>set()</code> assign by reference to whatever object they are passed. So if that object was previously copied (by a subassigning <code>&lt;-</code> or an explicit <code>copy(DT)</code>) then it's the copy that gets modified by reference.</p> <pre><code>DT &lt;- data.table(a = c(1, 2), b = c(11, 12)) newDT &lt;- DT .Internal(inspect(DT)) # @0000000003B7E2A0 19 VECSXP g0c7 [OBJ,NAM(2),ATT] (len=2, tl=100) # @00000000040C2288 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 1,2 # @00000000040C2250 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 11,12 # ATTRIB: # ..snip.. .Internal(inspect(newDT)) # precisely the same object at this point # @0000000003B7E2A0 19 VECSXP g0c7 [OBJ,NAM(2),ATT] (len=2, tl=100) # @00000000040C2288 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 1,2 # @00000000040C2250 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 11,12 # ATTRIB: # ..snip.. tracemem(newDT) # [1] "&lt;0x0000000003b7e2a0" newDT$b[2] &lt;- 200 # tracemem[0000000003B7E2A0 -&gt; 00000000040ED948]: # tracemem[00000000040ED948 -&gt; 00000000040ED830]: .Call copy $&lt;-.data.table $&lt;- .Internal(inspect(DT)) # @0000000003B7E2A0 19 VECSXP g0c7 [OBJ,NAM(2),TR,ATT] (len=2, tl=100) # @00000000040C2288 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 1,2 # @00000000040C2250 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 11,12 # ATTRIB: # ..snip.. .Internal(inspect(newDT)) # @0000000003D97A58 19 VECSXP g0c7 [OBJ,NAM(2),ATT] (len=2, tl=100) # @00000000040ED7F8 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 1,2 # @00000000040ED8D8 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 11,200 # ATTRIB: # ..snip.. </code></pre> <p>Notice how even the <code>a</code> vector was copied (different hex value indicates new copy of vector), even though <code>a</code> wasn't changed. Even the whole of <code>b</code> was copied, rather than just changing the elements that need to be changed. That's important to avoid for large data, and why <code>:=</code> and <code>set()</code> were introduced to <code>data.table</code>.</p> <p>Now, with our copied <code>newDT</code> we can modify it by reference :</p> <pre><code>newDT # a b # [1,] 1 11 # [2,] 2 200 newDT[2, b := 400] # a b # See FAQ 2.21 for why this prints newDT # [1,] 1 11 # [2,] 2 400 .Internal(inspect(newDT)) # @0000000003D97A58 19 VECSXP g0c7 [OBJ,NAM(2),ATT] (len=2, tl=100) # @00000000040ED7F8 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 1,2 # @00000000040ED8D8 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 11,400 # ATTRIB: # ..snip .. </code></pre> <p>Notice that all 3 hex values (the vector of column points, and each of the 2 columns) remain unchanged. So it was truly modified by reference with no copies at all.</p> <p>Or, we can modify the original <code>DT</code> by reference :</p> <pre><code>DT[2, b := 600] # a b # [1,] 1 11 # [2,] 2 600 .Internal(inspect(DT)) # @0000000003B7E2A0 19 VECSXP g0c7 [OBJ,NAM(2),ATT] (len=2, tl=100) # @00000000040C2288 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 1,2 # @00000000040C2250 14 REALSXP g0c2 [NAM(2)] (len=2, tl=0) 11,600 # ATTRIB: # ..snip.. </code></pre> <p>Those hex values are the same as the original values we saw for <code>DT</code> above. Type <code>example(copy)</code> for more examples using <code>tracemem</code> and comparison to <code>data.frame</code>.</p> <p>Btw, if you <code>tracemem(DT)</code> then <code>DT[2,b:=600]</code> you'll see one copy reported. That is a copy of the first 10 rows that the <code>print</code> method does. When wrapped with <code>invisible()</code> or when called within a function or script, the <code>print</code> method isn't called. </p> <p>All this applies inside functions too; i.e., <code>:=</code> and <code>set()</code> do not copy on write, even within functions. If you need to modify a local copy, then call <code>x=copy(x)</code> at the start of the function. But, remember <code>data.table</code> is for large data (as well as faster programming advantages for small data). We deliberately don't want to copy large objects (ever). As a result we don't need to allow for the usual 3* working memory factor rule of thumb. We try to only need working memory as large as one column (i.e. a working memory factor of 1/ncol rather than 3).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload