Let\'s say I have a data.table
set.seed(1) # to make the example reproducible ex<-data.table(AAA=runif(100000), BBB=runif(100000),
Looping over the combinations within data.table:
comblist <- combn(names(ex)[-5],2,FUN=list) res2 <- ex[,lapply(comblist,function(x) get(x[1])-get(x[2]))] setnames(res2,names(res2),sapply(comblist,paste,collapse="_"))