[datatable-help] setnames on copy data.table also renames original data.table object

Michael Nelson michael.nelson at sydney.edu.au
Mon Mar 10 06:20:34 CET 2014


This is well explained in the help file for `copy` (and `setnames`)

DT <- DT2

does *not* create a copy, it creates two object names that refer to the same object reference.

If you want force the creation of a copy, use `copy`

DT <- copy(DT2)

Then your example will work as expected.


________________________________
From: datatable-help-bounces at lists.r-forge.r-project.org [datatable-help-bounces at lists.r-forge.r-project.org] on behalf of Manabu Sakamoto [manabu.sakamoto at gmail.com]
Sent: Monday, 10 March 2014 4:00 PM
To: datatable-help at lists.r-forge.r-project.org
Subject: [datatable-help] setnames on copy data.table also renames original data.table object

Dear list,

I have a data.table object for instance DT:

x <- seq(1:100)
y <- x^2
DT <- data.table(X=x, Y=y)

and I produce a copy

DT2 <- DT

which I rename

setnames(DT2, c("A","B"))

this somehow also renames DT, the names of which are now "A" and "B".

How can I just rename the copy and keep the names of the original?

Many thanks,
Manabu

--
Manabu Sakamoto, PhD
manabu.sakamoto at gmail.com<mailto:manabu.sakamoto at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20140310/a7bcc7c9/attachment-0001.html>


More information about the datatable-help mailing list