我想对我的data.table
x
中的列重新排序,给定一个列名称的字符向量,neworder
:
library(data.table)
x <- data.table(a = 1:3, b = 3:1, c = runif(3))
neworder <- c("c", "b", "a")
显然我能做到:
x[ , neworder, with = FALSE]
# or
x[ , ..neworder]
# c b a
# 1: 0.8476623 3 1
# 2: 0.4787768 2 2
# 3: 0.3570803 1 3
但这需要再次复制整个数据集.还有别的方法吗?