Skip to content

Instantly share code, notes, and snippets.

@aousabdo
Created December 30, 2014 19:03
Show Gist options
  • Save aousabdo/20977bc773043c1836e0 to your computer and use it in GitHub Desktop.
Save aousabdo/20977bc773043c1836e0 to your computer and use it in GitHub Desktop.
# start clean
rm(list=ls())
# load libraries
library(data.table)
DT <- data.table(A = rnorm(10), B = rnorm(10))
# this gives lots of warnings, there seems to be a better way of doing this
names(DT) <- tolower(names(DT))
# same with this line
names(DT) <- gsub(" ", ".", names(DT))
@arunsrinivasan
Copy link

What's your data.table version and R version? And what's the warning? I'm on R3.1, data.table 1.9.5 (current devel) and works fine.

@aousabdo
Copy link
Author

R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
data.table 1.9.4

Warning:

Warning messages:
1: package ‘data.table’ was built under R version 3.1.2
2: In names<-.data.table(*tmp*, value = c("a", "b")) :
The names(x)<-value syntax copies the whole table. This is due to <- in R itself. Please change to setnames(x,old,new) which does not copy and is faster. See help('setnames'). You can safely ignore this warning if it is inconvenient to change right now. Setting options(warn=2) turns this warning into an error, so you can then use traceback() to find and change your names<- calls.
3: In names<-.data.table(*tmp*, value = c("a", "b")) :
The names(x)<-value syntax copies the whole table. This is due to <- in R itself. Please change to setnames(x,old,new) which does not copy and is faster. See help('setnames'). You can safely ignore this warning if it is inconvenient to change right now. Setting options(warn=2) turns this warning into an error, so you can then use traceback() to find and change your names<- calls.

@aousabdo
Copy link
Author

updating data.table to dev version 1.9.5 fixes the problem.

@arunsrinivasan
Copy link

The error message says that it's better to use setnames() instead of names<-. This was meant for older versions of R which copied the entire data.frame/data.table to just replace names. Improvements were made in Rv3.1+. But still the idiomatic way would be to use setnames().

HTH

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