我想重新排列
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
但这将需要再次复制整个数据集。还有其他方法可以做到这一点吗?
setcolorder
只是移动列指针,根本不使用任何工作内存。关于使用与一列大小相同的工作内存的那句话只是在讲setkey
而已。 - Matt Dowlesetcolorder(df, c("someCol",colnames(dt)[!(colnames(dt) %in% c("someCol"))]))
- hedgedandleveredsetcolorder()
现在可以接受少于ncol(DT)
列移动到前面"。 - Henrik