R, data.table: Sum all columns whose names are stored in a vector

后端 未结 1 1373
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-20 01:18

From a data.table d such as for example

require(data.table)
d = data.table(a = 1:4, b = 11:14, c = 21:24, group = c(1,1,2,2))

1条回答
  •  逝去的感伤
    2021-01-20 02:09

    d[, lapply(.SD, sum), by = group, .SDcols = varsToSum]
    
       group a  b
    1:     1 3 23
    2:     2 7 27
    

    0 讨论(0)
提交回复
热议问题