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: [email protected] 
[[email protected]] on behalf of Manabu 
Sakamoto [[email protected]]
Sent: Monday, 10 March 2014 4:00 PM
To: [email protected]
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
[email protected]<mailto:[email protected]>
_______________________________________________
datatable-help mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help

Reply via email to