0

I'd like to create an immutable vector of a data.table object's names, which won't change after using setnames. For example, I want to create a version of mtcars.names that will stick with "mpg" instead of "miles.per.gallon" here:

mtcars.dt <- data.table(mtcars)
(mtcars.names <- names(mtcars.dt))
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"
setnames(mtcars.dt, "mpg", "miles.per.gallon")
mtcars.names
 [1] "miles.per.gallon" "cyl"              "disp"             "hp"
 [5] "drat"             "wt"               "qsec"             "vs"
 [9] "am"               "gear"             "carb"
Max Ghenis
  • 14,783
  • 16
  • 84
  • 132

1 Answers1

3
mtcars.names <- copy(names(mtcars.dt))
setnames(mtcars.dt, "mpg", "miles.per.gallon")
mtcars.names
## [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
## [11] "carb"
Jake Burkhead
  • 6,435
  • 2
  • 21
  • 32