You can use the setcolorder() function from the data.table package.
According to R Documentation:
setcolorder
reorders the columns of data.table, by reference, to the new order provided
The basic syntax of setcolorder is as follows:
setcolorder(x, neworder=key(x))
Arguments
x
A data.table.
neworder
A character vector of the new column name ordering. May also be column numbers. If length(neworder) < length(x), the specified columns are moved in order to the "front" of x. By default, setcolorder without a specified neworder moves the key columns to the "front" of x.
In your case:
library(data.table)
> x <- data.table(a = 1:3, b = 3:1, c = runif(3))
> x
a b c
1: 1 3 0.0456276
2: 2 2 0.4333822
3: 3 1 0.1896660
> setcolorder(x, c("c", "b", "a"))
> x
c b a
1: 0.0456276 3 1
2: 0.4333822 2 2
3: 0.1896660 1 3