<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix"><br>
Yes that's correct behaviour and desirable/crucial. The set*
functions and := do not copy-on-write, unlike base. They work by
reference.<br>
<br>
See ?copy and type example(copy) at the prompt.<br>
<br>
If you *really* want to copy a 20GB in RAM, use DT2 <-
copy(DT)<br>
<br>
<a class="moz-txt-link-freetext" href="http://stackoverflow.com/questions/10225098/understanding-exactly-when-a-data-table-is-a-reference-to-vs-a-copy-of-another">http://stackoverflow.com/questions/10225098/understanding-exactly-when-a-data-table-is-a-reference-to-vs-a-copy-of-another</a><br>
<br>
Matt<br>
<br>
On 11/01/14 14:18, Holger Kirsten wrote:<br>
</div>
<blockquote
cite="mid:2175122676-15998@hogwarts.imise.uni-leipzig.de"
type="cite">In a debugging session, I found that setnames changed
the names of an identical data.table although having a different
name><br>
<br>
<font size="2">> ############### using setnames()<br>
> require(data.table)<br>
> mytab = data.table(a = letters[1:4], b = 1:4 )<br>
> str(mytab)<br>
Classes ‘data.table’ and 'data.frame': 4 obs. of 2
variables:<br>
$ a: chr "a" "b" "c" "d"<br>
$ b: int 1 2 3 4<br>
- attr(*, ".internal.selfref")=<externalptr> <br>
> mytab<br>
a b<br>
1: a 1<br>
2: b 2<br>
3: c 3<br>
4: d 4<br>
> <br>
> othertab = mytab<br>
> othertab<br>
a b<br>
1: a 1<br>
2: b 2<br>
3: c 3<br>
4: d 4<br>
> setnames(othertab, c("a", "b"), c("aa","bb"))<br>
> othertab<br>
aa bb<br>
1: a 1<br>
2: b 2<br>
3: c 3<br>
4: d 4<br>
> mytab ## names have unexpectedly changed too<br>
aa bb<br>
1: a 1<br>
2: b 2<br>
3: c 3<br>
4: d 4<br>
> <br>
> ############### using names()<br>
> mytab = data.table(a = letters[1:4], b = 1:4 )<br>
> str(mytab)<br>
Classes ‘data.table’ and 'data.frame': 4 obs. of 2
variables:<br>
$ a: chr "a" "b" "c" "d"<br>
$ b: int 1 2 3 4<br>
- attr(*, ".internal.selfref")=<externalptr> <br>
> mytab<br>
a b<br>
1: a 1<br>
2: b 2<br>
3: c 3<br>
4: d 4<br>
> <br>
> othertab = mytab<br>
> othertab<br>
a b<br>
1: a 1<br>
2: b 2<br>
3: c 3<br>
4: d 4<br>
> names(othertab) = c("aa","bb")<br>
Warning message:<br>
In `names<-.data.table`(`*tmp*`, value = c("aa", "bb")) :<br>
The names(x)<-value syntax copies the whole table. This is
due to <- in R itself. Please change to setnames(x,old,new)
which does not copy and is faster. See help('setnames'). You can
safely ignore this warning if it is inconvenient to change right
now. Setting options(warn=2) turns this warning into an error,
so you can then use traceback() to find and change your
names<- calls.<br>
> othertab<br>
aa bb<br>
1: a 1<br>
2: b 2<br>
3: c 3<br>
4: d 4<br>
> mytab ## names unchanged as expected<br>
a b<br>
1: a 1<br>
2: b 2<br>
3: c 3<br>
4: d 4<br>
> <br>
> sessionInfo()<br>
R version 3.0.1 (2013-05-16)<br>
Platform: x86_64-w64-mingw32/x64 (64-bit)<br>
<br>
locale:<br>
[1] LC_COLLATE=German_Germany.1252
LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252
LC_NUMERIC=C LC_TIME=German_Germany.1252 <br>
<br>
attached base packages:<br>
[1] stats graphics grDevices utils datasets methods
base <br>
<br>
other attached packages:<br>
[1] data.table_1.8.10<br>
<br>
loaded via a namespace (and not attached):<br>
[1] tools_3.0.1</font><br>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
datatable-help mailing list
<a class="moz-txt-link-abbreviated" href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></pre>
</blockquote>
<br>
</body>
</html>