<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" id="owaParaStyle"></style>
</head>
<body fpstyle="1" ocsi="0">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">This is well explained in the help file for `copy` (and `setnames`)
<div><br>
</div>
<div>DT <- DT2 </div>
<div><br>
</div>
<div>does *not* create a copy, it creates two object names that refer to the same object reference.</div>
<div><br>
</div>
<div>If you want force the creation of a copy, use `copy`</div>
<div><br>
</div>
<div>DT <- copy(DT2)</div>
<div><br>
</div>
<div>Then your example will work as expected.</div>
<div><br>
</div>
<div><br>
<div style="font-family: Times New Roman; color: #000000; font-size: 16px">
<hr tabindex="-1">
<div id="divRpF47660" style="direction: ltr;"><font face="Tahoma" size="2" color="#000000"><b>From:</b> datatable-help-bounces@lists.r-forge.r-project.org [datatable-help-bounces@lists.r-forge.r-project.org] on behalf of Manabu Sakamoto [manabu.sakamoto@gmail.com]<br>
<b>Sent:</b> Monday, 10 March 2014 4:00 PM<br>
<b>To:</b> datatable-help@lists.r-forge.r-project.org<br>
<b>Subject:</b> [datatable-help] setnames on copy data.table also renames original data.table object<br>
</font><br>
</div>
<div></div>
<div>
<div dir="ltr">Dear list,
<div><br>
</div>
<div>I have a data.table object for instance DT:</div>
<div><br>
</div>
<div>x <- seq(1:100)</div>
<div>y <- x^2</div>
<div>DT <- data.table(X=x, Y=y)</div>
<div><br>
</div>
<div>and I produce a copy</div>
<div><br>
</div>
<div>DT2 <- DT</div>
<div><br>
</div>
<div>which I rename</div>
<div><br>
</div>
<div>setnames(DT2, c("A","B"))</div>
<div><br>
</div>
<div>this somehow also renames DT, the names of which are now "A" and "B".</div>
<div><br>
</div>
<div>How can I just rename the copy and keep the names of the original?</div>
<div><br>
</div>
<div>Many thanks,</div>
<div>Manabu<br clear="all">
<div><br>
</div>
-- <br>
<div dir="ltr">Manabu Sakamoto, PhD
<div><a href="mailto:manabu.sakamoto@gmail.com" target="_blank">manabu.sakamoto@gmail.com</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>