Group by multiple columns and sum other multiple columns

后端 未结 7 542
孤城傲影
孤城傲影 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:07

    This seems like a task for ddply (I use the 'baseball' dataset which is included with plyr):

    library(plyr)
    groupColumns = c("year","team")
    dataColumns = c("hr", "rbi","sb")
    res = ddply(baseball, groupColumns, function(x) colSums(x[dataColumns]))
    head(res)
    

    This gives per groupColumns the sum of the columns specified in dataColumns.

提交回复
热议问题