Skip to content

setnames overwrites previous copy of names(dt) #2136

Closed

Description

Hi,

In summary, setnames(dt) seems to overwrite previous copies of names(dt). Found in R-3.3 and R-3.4.

R version 3.4.0 (2017-04-21) -- "You Stupid Darkness"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

library(data.table)
data.table 1.10.4
The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way
Documentation: ?data.table, example(data.table) and browseVignettes("data.table")
Release notes, videos and slides: http://r-datatable.com
Warning message:
R graphics engine version 12 is not supported by this version of RStudio. The Plots tab will be disabled until a newer version of RStudio is installed.

This Works as expected

df = data.frame(foo=c(1,2,3), bar=c(3,4,5))
cnames_old = names(df)
cnames_old
[1] "foo" "bar"
names(df) = c("FOO","BAR")
colnames(df)
[1] "FOO" "BAR"
cnames_old
[1] "foo" "bar"

Here names(dt) <- new_names also works as expected

dt = as.data.table(data.frame(foo=c(1,2,3), bar=c(3,4,5)))
cnames_old_dt = names(dt)
tracemem(cnames_old_dt)
[1] "<0x64b3da0>"
cnames_old_dt
[1] "foo" "bar"
names(dt) = c("foo_dt","bar_dt")
names(dt)
[1] "foo_dt" "bar_dt"
cnames_old_dt
[1] "foo" "bar"

Setnames() changes copy of names(dt) generated before call to setnames()

dt = as.data.table(data.frame(foo=c(1,2,3), bar=c(3,4,5)))
cnames_old_dt = names(dt)
tracemem(cnames_old_dt)
[1] "<0x64afd20>"
cnames_old_dt
[1] "foo" "bar"
setnames(dt, c("foo","bar"), c("foo_dt","bar_dt") )
names(dt)
[1] "foo_dt" "bar_dt"
cnames_old_dt
[1] "foo_dt" "bar_dt"

Best regards,
Hilmar

Activity

  1. franknarf1 commented on May 2, 2017

    @franknarf1
    Contributor

    Known behavior: http://stackoverflow.com/a/15913648/ Maybe it belongs in the FAQ, though.

  2. MPIIB-Department-TFMeyer commented on May 2, 2017

    @MPIIB-Department-TFMeyer
    Author

    Thanks for the reference, that explains a lot. Still, it is not clear why one should expect names(dt) to behave differently to setnames() in this respect. Technically it might be because names()<- assigns a new vector. In terms of usage it is quite surprising to find a package changing the way variables are passed in R, although I understand that there might be reasons to do so.

  3. franknarf1 commented on May 2, 2017

    @franknarf1
    Contributor

    R has "copy-on-modify semantics", so until it knows that x in x <- y has been modified, they are both pointing to the same object. Data.table's set* and := operators all modify by reference so R does not recognize the need to make a copy. In contrast, R recognizes that it should make a copy with names<-. Modification by reference is one of the main selling points of the package, but you can pretty much always avoid that behavior with copy().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions