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

Matt Dowle mdowle at mdowle.plus.com
Mon Mar 10 14:07:14 CET 2014


And more info here (see both answers) :
http://stackoverflow.com/questions/10225098/understanding-exactly-when-a-data-table-is-a-reference-to-vs-a-copy-of-another

On 10/03/14 05:20, Michael Nelson wrote:
> 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>
>
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20140310/eb7c182e/attachment.html>


More information about the datatable-help mailing list