r - passing variables as data.table column names

后端 未结 1 2004
天涯浪人
天涯浪人 2021-01-12 09:30

The more I use it, the more data.table is replacing dplyr as my \'goto\' package as the speed it offers is a big plus.

Question

相关标签:
1条回答
  • 2021-01-12 10:14

    We can use eval(as.name(..

    dt[eval(as.name(myCol[1]))> eval(as.name(myCol[2]))]
    

    Or we can specify it in the .SDcols

    dt[dt[, .I[.SD[[1]]> .SD[[2]]], .SDcols= myCol]]
    

    Or an option using get by @thelatemail

    dt[get(myCol[1]) > get(myCol[2])]
    

    If there are only two elements, we can also use Reduce with mget (a slight variation of @thelatemail's answer)

    dt[Reduce('>', mget(myCol))]
    
    0 讨论(0)
提交回复
热议问题