Probably a more elegant way to do this, and it doesn't scale well, but here it goes ...
dt[,A1:=lapply(A,'[[',1)] dt[,A2:=lapply(A,'[[',2)] dt[B==1,`:=`(Bnew=A1,Cnew=A2)] dt[B==2,`:=`(Bnew=A2,Cnew=A1)] dt[,`:=`(A1=NULL,A2=NULL,B=NULL,C=NULL)] setnames(dt,c("Bnew","Cnew"),c("B","C"))
Dean MacGregor
source share