像这样的东西应该工作
DT <- data.table(class = sample(1:3, 10, TRUE), v1 =sample(10), v2 = sample(10), v3 = sample(10))
DT
class v1 v2 v3
1: 1 4 6 6
2: 1 7 1 5
3: 1 5 5 10
4: 1 3 8 7
5: 3 8 4 3
6: 3 9 7 9
7: 2 1 3 8
8: 2 10 10 2
9: 1 2 2 4
10: 2 6 9 1
# the neworder column contains the new permutations
swapcols <- data.table(class = 1:3, neworder = list(c(1,2,3), c(3,1,2),c(1,3,2)))
setkey(DT, class)
setkey(swapcols, class)
DT[swapcols, setNames(list(v1,v2,v3)[unlist(neworder)], c('v1','v2','v3'))]
class v1 v2 v3
1: 1 4 6 6
2: 1 7 1 5
3: 1 5 5 10
4: 1 3 8 7
5: 1 2 2 4
6: 2 8 1 3
7: 2 2 10 10
8: 2 1 6 9
9: 3 8 3 4
10: 3 9 9 7
做类似的事情可能会更有效率
DT[swapcols, setcolorder(.SD, unlist(neworder))]
或者
new <- DT[swapcols, list(v1,v2,v3)[unlist(neworder)]]
setnames(new, names(new), c('class', c('v1','v2','v3'))
你也可以使用:=
. 就像是
DT[J(1), `:=`(v1= v2,v2=v3,v3=v1)]
您可以尝试在函数中自动执行此操作的某种方式,但这将是 eval / parse 和 do.call 的混乱
来自 Matthew(在 v1.8.3 中测试):
DT = data.table(class=c(1,2,1),v1=c(10,2,70),v2=c(3,24,3),v3=c(8,7,9))
DT
class v1 v2 v3
1: 1 10 3 8
2: 2 2 24 7
3: 1 70 3 9
perm = c(3,1,2)
DT[class==1, names(DT)[-1L] := .SD, .SDcols = perm+1L]]
DT
class v1 v2 v3
1: 1 8 10 3
2: 2 2 24 7
3: 1 9 70 3