[datatable-help] setnames on a non-data.table object

Matt Dowle mdowle at mdowle.plus.com
Wed Oct 2 08:39:55 CEST 2013


Hi,

There's no technical reason.  I guess enough people realise now that the 
set* functions change the object by reference. So if setnames worked on 
data.frame :

    DF1 = data.frame(a=1:3, b=4:6)
    DF2 = DF1
    setnames(DF2, "b", "B")

This would change both DF1 and DF2.  There might be someone who throws 
up their hands in horror and says this breaks everything they've known 
about data.frame, too. Isn't it enough that data.table breaks everything 
already?

We'd have to take a deep breath and calmly explain copy() is needed :

    DF1 = data.frame(a=1:3, b=4:6)
    DF2 = copy(DF1)
    setnames(DF2, "b", "B")

So the reason setnames() hasn't so far been enabled for data.frame is 
just for safety (using it on a data.frame accidentally) and to avoid 
complaints and negative Twitterers.   On the other hand setnames 
(different from setNames) is a data.table function so it's not like 
we're overloading <- or anything.

I suppose setnames() could copy the whole DF2 just like base.  But that 
defeats it's purpose, set* functions work by reference. setnames() is a 
little different in that it's more convenient and safer than base 
syntax, too, though; e.g., changing a column name by name.  So I can see 
someone might want to use it for that reason alone and not mind it 
copies the whole DF when passed a DF.

Matt


On 01/10/13 20:51, Ricardo Saporta wrote:
> Hi All,
>
> I'm wondering if there are any potential problems or unforseen 
> pitfalls with having
>
>   setnames(x, nms)
>
> call
>    setattr(x, "names", nms)
>
> when x is not a data.table.
>
> Thoughts?
>
> Rick
>
> Ricardo Saporta
> Graduate Student, Data Analytics
> Rutgers University, New Jersey
> e: saporta at rutgers.edu <mailto:saporta at rutgers.edu>
>
>
>
> _______________________________________________
> 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/20131002/af22c655/attachment.html>


More information about the datatable-help mailing list