data.table Adding and modifying columns Reordering columns


Example

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

To rearrange the order of columns, use setcolorder. For example, to reverse them

setcolorder(DT, rev(names(DT)))

This costs almost nothing in terms of performance, since it is just permuting the list of column pointers in the data.table.