像這樣的東西應該工作
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 /解析的混亂和do.call
從馬修(在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)[-1]:=.SD[,perm+1,with=FALSE]]
DT
class v1 v2 v3
1: 1 8 10 3
2: 2 2 24 7
3: 1 9 70 3
我相信有一個錯字。在倒數第二句中,你的意思是'data.table'而不是'data.frame',對吧? – Maiasaura
更正!謝謝 – amir