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"