Group by multiple columns and sum other multiple columns

后端 未结 7 538
孤城傲影
孤城傲影 2020-11-22 07:35

I have a data frame with about 200 columns, out of them I want to group the table by first 10 or so which are factors and sum the rest of the columns.

I have list of

相关标签:
7条回答
  • 2020-11-22 08:33

    The data.table way is :

    DT[, lapply(.SD,sum), by=list(col1,col2,col3,...)]
    

    or

    DT[, lapply(.SD,sum), by=colnames(DT)[1:10]]
    

    where .SD is the (S)ubset of (D)ata excluding group columns. (Aside: If you need to refer to group columns generically, they are in .BY.)

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