[R] Confusing behaviour in data.table: unexpectedly changing variable

Jonathan Dushoff dushoff at mcmaster.ca
Wed Sep 25 01:47:25 CEST 2013


I got bitten badly when a variable I created for the purpose of
recording an old set of names changed when I didn't think I was going
near it.

I'm not sure if this is a desired behaviour, or documented, or warned
about.  I read the data.table intro and the FAQ, and also ?setnames.

Ben Bolker created a minimal reproducible example:

library(data.table)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
names(DT)
## [1] "x" "y" "v"

oldnames <- names(DT)
print(oldnames)
## [1] "x" "y" "v"

setnames(DT, LETTERS[1:3])
print(oldnames)
## [1] "A" "B" "C"

-- 
McMaster University Department of Biology
http://lalashan.mcmaster.ca/theobio/DushoffLab/index.php/Main_Page
https://twitter.com/jd_mathbio



More information about the R-help mailing list