<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><p>Thanks for reporting. That's expected behaviour. Use an explicit <code>copy</code>.</p>
<p>In short, when you do: <code>DT1 <- DT2</code>, there's <strong><code>no copy</code></strong> being made. They still reference/point to the same location (try doing <code>tracemem(DT1)</code> and <code>tracemem(DT2)</code>).</p>
<p>So when you change the names of one <code>DT</code> by reference, the other one will get changed as well - they're both pointing to the same location.</p>
<p>To overcome this, when you want to duplicate a <code>DT</code>, explicitly use <code>copy</code>. That is, <code>DT1 <- copy(DT2)</code>. Now if you <code>setnames(DT1, c("x", "y"))</code>, then <code>DT2</code> names won't get changed.</p>
<p>I think there's a FR somewhere on documenting this… Thanks again for reporting (with nice example).</p>
<p><style>body{font-family:Helvetica,Arial;font-size:13px}</style><style>body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
padding:1em;
margin:auto;
background:#fefefe;
}
h1, h2, h3, h4, h5, h6 {
font-weight: bold;
}
h1 {
color: #000000;
font-size: 28pt;
}
h2 {
border-bottom: 1px solid #CCCCCC;
color: #000000;
font-size: 24px;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777777;
background-color: inherit;
font-size: 14px;
}
hr {
height: 0.2em;
border: 0;
color: #CCCCCC;
background-color: #CCCCCC;
}
p, blockquote, ul, ol, dl, li, table, pre {
margin: 15px 0;
}
a, a:visited {
color: #4183C4;
background-color: inherit;
text-decoration: none;
}
#message {
border-radius: 6px;
border: 1px solid #ccc;
display:block;
width:100%;
height:60px;
margin:6px 0px;
}
button, #ws {
font-size: 12 pt;
padding: 4px 6px;
border-radius: 5px;
border: 1px solid #bbb;
background-color: #eee;
}
code, pre, #ws, #message {
font-family: Monaco;
font-size: 10pt;
border-radius: 3px;
background-color: #F8F8F8;
color: inherit;
}
code {
border: 1px solid #EAEAEA;
margin: 0 2px;
padding: 0 5px;
}
pre {
border: 1px solid #CCCCCC;
overflow: auto;
padding: 4px 8px;
}
pre > code {
border: 0;
margin: 0;
padding: 0;
}
#ws { background-color: #f8f8f8; }
.send { color:#77bb77; }
.server { color:#7799bb; }
.error { color:#AA0000; }</style></p><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div> <div id="bloop_sign_1389450647203549952" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">Arun</div></div> <div style="color:gray"><hr style="style:linear">From: <span style="color:black">Holger Kirsten</span> <a href="mailto:hkirsten@imise.uni-leipzig.de">Holger Kirsten</a><br>Reply: <span style="color:black">Holger Kirsten</span> <a href="mailto:hkirsten@imise.uni-leipzig.de">hkirsten@imise.uni-leipzig.de</a><br>Date: <span style="color:black">January 11, 2014 at 3:19:02 PM</span><br>To: <span style="color:black">datatable-help@lists.r-forge.r-project.org</span> <a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a><br>Subject: <span style="color:black"> [datatable-help] setnames changes names of other data.table <br></span></div> <blockquote type="cite" class="clean_bq"><span><div><div>
<title></title>
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>datatable-help mailing list
<br>datatable-help@lists.r-forge.r-project.org
<br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</div></div></span></blockquote><p></p></body></html>