data.table Adding and modifying columns Renaming columns


# example data
DT =, 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.