<div dir="ltr">As a fan of your work I have always been curious if you are aware of this? Â I find it causes new users to make mistakes.<div><br></div><div><br></div><div><div>> dt = list()</div><div>> dt$x = 1:10</div>
<div>> dt$y = letters[10:1]</div><div>> dt = as.data.table(as.data.frame(dt))</div><div>> dt</div><div>  Â  Â x y</div><div> 1: Â 1 j</div><div> 2: Â 2 i</div><div> 3: Â 3 h</div><div> 4: Â 4 g</div><div> 5: Â 5 f</div>
<div> 6: Â 6 e</div><div> 7: Â 7 d</div><div> 8: Â 8 c</div><div> 9: Â 9 b</div><div>10: 10 a</div><div>> x0 = dt$x</div><div>> x1 = dt$x</div><div>> x0[1] = 11</div><div>> setkeyv(dt,"y")</div><div>> x0</div>
<div> [1] 11 Â 2 Â 3 Â 4 Â 5 Â 6 Â 7 Â 8 Â 9 10</div><div>> x1</div><div> [1] 10 Â 9 Â 8 Â 7 Â 6 Â 5 Â 4 Â 3 Â 2 Â 1</div><div>> x1 == x0</div><div> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE</div></div><div><br>
</div><div><br></div><div>x0 and x1 have assignments at the same exact time, and since R data.frame's will not do this, it lures people into thinking they are then identical and distinct as they are with data.frame's. Â My theory is they are not actually copied: they are promised. Â When x0 has its index 1 changed it induces a copy distinct from dt$x, but x1 has had no operation on it so it refers to dt$x with its promise. Setting the key on dt reorders it and since x1 still hasn't been evaluated it now matches the order of dt.</div>
<div><br></div><div>I found new users getting unpredictable results because they would try to use a data.table as a data.frame and induce this with sorts. Â If you thought you copied something in a particular order in dt by doing the assigning ahead of the setkeyv you make a mistake. Â  You don't really expect x1 assigned maybe a page of code above to have its order changed by a setkeyv. Â You do if you think about C pointers and references, but in R you really don't think that way. Â Many R users don't even know what a pointer is.</div>
<div><br></div><div><br></div><div>Thanks,</div><div>Jeremiah</div><div><br></div><div><div>> sessionInfo()</div><div>R version 3.0.1 (2013-05-16)</div><div>Platform: x86_64-unknown-linux-gnu (64-bit)</div><div><br></div>
<div>locale:</div><div> [1] LC_CTYPE=en_US.UTF-8 Â  Â  Â  LC_NUMERIC=C Â  Â  Â  Â  Â  Â  Â </div><div> [3] LC_TIME=en_US.UTF-8 Â  Â  Â  Â LC_COLLATE=en_US.UTF-8 Â  Â </div><div> [5] LC_MONETARY=en_US.UTF-8 Â  Â LC_MESSAGES=en_US.UTF-8 Â Â </div>
<div> [7] LC_PAPER=C Â  Â  Â  Â  Â  Â  Â  Â  LC_NAME=C Â  Â  Â  Â  Â  Â  Â  Â Â </div><div> [9] LC_ADDRESS=C Â  Â  Â  Â  Â  Â  Â  LC_TELEPHONE=C Â  Â  Â  Â  Â  Â </div><div>[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C Â  Â  Â Â </div><div><br></div>
<div>attached base packages:</div><div>[1] splines Â  parallel Â stats Â  Â  graphics Â grDevices utils Â  Â  datasets </div><div>[8] methods Â  base Â  Â Â </div><div><br></div><div>other attached packages:</div><div>[1] locfit_1.5-9.1 Â  Â  Â  edgeR_3.4.2 Â  Â  Â  Â  Â limma_3.18.13 Â  Â  Â Â </div>
<div>[4] data.table_1.9.2 Â  Â  GenomicRanges_1.14.4 XVector_0.2.0 Â  Â  Â Â </div><div>[7] IRanges_1.20.7 Â  Â  Â  BiocGenerics_0.8.0 Â </div><div><br></div><div>loaded via a namespace (and not attached):</div><div>[1] grid_3.0.1 Â  Â  Â lattice_0.20-15 plyr_1.8.1 Â  Â  Â Rcpp_0.11.1 Â  Â </div>
<div>[5] reshape2_1.4 Â  Â stats4_3.0.1 Â  Â stringr_0.6.2 Â  tools_3.0.1 Â  Â </div></div><div><br></div><div><br></div><div><br></div></div>