Subtract every column from each other column in a R data.table

后端 未结 2 1639
[愿得一人]
[愿得一人] 2020-12-17 22:07

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),
                 


        
2条回答
  •  情话喂你
    2020-12-17 22:33

    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="_"))
    

提交回复
热议问题