Sum multiple columns [duplicate]

↘锁芯ラ 提交于 2020-01-03 05:31:28

问题


I am trying to write a function that will sum the column(s) in the data frame according to the values in the first two columns.For example I have a matrix M,

Crs gr  P_7 P_8      
38  1   3   16
38  1   12  45
38  1   9   28
40  2   3   9
40  2   14  29
40  1   4   3
40  2   8   2

I want to sum the columns according to column1(crs) first and then column2(gr). Result will be,

    Crs gr  P_7  P_8      
    38  1   24  89
    40  2   25  40
    40  1   4   3

Currently I am using,

M <- M[, list(sum(P_7),sum(P_8)), by=list(Crs,gr)]

But the problem with this, is that I have to define the names of columns which wont be fixed. So, I was wondering how can I do this without defining the names of the columns. Thanks in advance!


回答1:


You're looking for this:

M[, lapply(.SD, sum), by = list(Crs, gr)]



回答2:


The package plyr has some magic for situations just like this. Use a combination of ddply and numcolwise, like this:

library(plyr)
ddply(dat, .(Crs, gr), numcolwise(sum))

results in:

  Crs gr P_7 P_8
1  38  1  24  89
2  40  1   4   3
3  40  2  25  40


来源:https://stackoverflow.com/questions/17407506/sum-multiple-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!