data.table Adding and modifying columns Renaming columns

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any data.table Question?