<html><head><style type=text/css><!--
.mcnt {word-wrap:break-word;-webkit-nbsp-mode:space;-webkit-line-break:after-white-space;}
.mcnt {font-family:Helvetica,Arial;font-size:13px;}
.mcnt {font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;padding:1em;margin:auto;background:#fefefe;}
.mcnt h1, .mcnt h2, .mcnt h3, .mcnt h4, .mcnt h5, .mcnt h6 {font-weight:bold;}
.mcnt h1 {color:#000000;font-size:28pt;}
.mcnt h2 {border-bottom:1px solid #CCCCCC;color:#000000;font-size:24px;}
.mcnt h3 {font-size:18px;}
.mcnt h4 {font-size:16px;}
.mcnt h5 {font-size:14px;}
.mcnt h6 {color:#777777;background-color:inherit;font-size:14px;}
.mcnt hr {height:0.2em;border:0;color:#CCCCCC;background-color:#CCCCCC;}
.mcnt p, .mcnt blockquote, .mcnt ul, .mcnt ol, .mcnt dl, .mcnt li, .mcnt table, .mcnt pre {margin:15px 0;}
.mcnt a, .mcnt a:visited {color:#4183C4;background-color:inherit;text-decoration:none;}
.mcnt #mcntmessage {border-radius:6px;border:1px solid #ccc;display:block;width:100%;height:60px;margin:6px 0px;}
.mcnt button, .mcnt #mcntws {font-size:12 pt;padding:4px 6px;border-radius:5px;border:1px solid #bbb;background-color:#eee;}
.mcnt code, .mcnt pre, .mcnt #mcntws, .mcnt #mcntmessage {font-family:Monaco;font-size:10pt;border-radius:3px;background-color:#F8F8F8;color:inherit;}
.mcnt code {border:1px solid #EAEAEA;margin:0 2px;padding:0 5px;}
.mcnt pre {border:1px solid #CCCCCC;overflow:auto;padding:4px 8px;}
.mcnt pre > code {border:0;margin:0;padding:0;}
.mcnt #mcntws {background-color:#f8f8f8;}
.mcnt .mcntsend {color:#77bb77;}
.mcnt .mcntserver {color:#7799bb;}
.mcnt .mcnterror {color:#AA0000;}
--></style></head><body><!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>DE</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:DontVertAlignCellWithSp/>
<w:DontBreakConstrainedForcedTables/>
<w:DontVertAlignInTxbx/>
<w:Word11KerningPairs/>
<w:CachedColBalance/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
<m:mathPr>
<m:mathFont m:val="Cambria Math"/>
<m:brkBin m:val="before"/>
<m:brkBinSub m:val="--"/>
<m:smallFrac m:val="off"/>
<m:dispDef/>
<m:lMargin m:val="0"/>
<m:rMargin m:val="0"/>
<m:defJc m:val="centerGroup"/>
<m:wrapIndent m:val="1440"/>
<m:intLim m:val="subSup"/>
<m:naryLim m:val="undOvr"/>
</m:mathPr></w:WordDocument>
</xml><![endif]-->
<p class="MsoNormal"><span style="mso-ansi-language:EN-US" lang="EN-US">Thanks for
the immediate answer! Now, everything is clear for me.<br></span></p>
<br><br><br><div><span title="aragorn168b@gmail.com">Arunkumar Srinivasan</span><span class="detail"> <aragorn168b@gmail.com></span> , 11.01.2014 15:31:<br><blockquote class="mcnt mori" style="margin:0 0 0 .8ex;border-left:2px blue solid;padding-left:1ex;"><div class="mcnt"><div><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>
<div id="mcntbloop_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="mcntbloop_sign_1389450647203549952" class="mcntbloop_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" class="mailto">Holger Kirsten</a><br>Reply: <span style="color:black;">Holger Kirsten</span> <a href="mailto:hkirsten@imise.uni-leipzig.de" class="mailto">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;"><a href="mailto:datatable-help@lists.r-forge.r-project.org" class="mailto">datatable-help@lists.r-forge.r-project.org</a></span> <a href="mailto:datatable-help@lists.r-forge.r-project.org" class="mailto">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="mcntclean_bq"><span><div>
In a debugging session, I found that setnames changed the names of
an identical data.table although having a different name><br>
<br>
<span style="font-size:small;">> ############### 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</span><br>
_______________________________________________
<br>datatable-help mailing list
<br><a href="mailto:datatable-help@lists.r-forge.r-project.org" class="mailto">datatable-help@lists.r-forge.r-project.org</a>
<br><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></div></span></blockquote></div></div></blockquote></div></body></html>