Repository navigation
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
franknarf1 commentedon May 2, 2017
Known behavior: http://stackoverflow.com/a/15913648/ Maybe it belongs in the FAQ, though.
MPIIB-Department-TFMeyer commentedon May 2, 2017
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.
franknarf1 commentedon May 2, 2017
R has "copy-on-modify semantics", so until it knows that
xinx <- yhas been modified, they are both pointing to the same object. Data.table'sset*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 withnames<-. Modification by reference is one of the main selling points of the package, but you can pretty much always avoid that behavior withcopy().