data.table Renaming columns


Example

# example data
DT = as.data.table(mtcars, keep.rownames = TRUE)

To rename a column (while keeping its data the same), there is no need to copy the data to a column with a new name and delete the old one. Instead, we can use

setnames(DT, "mpg_sq", "mpq_squared")

to modify the original column by reference.