How to sum over diagonals of data frame

后端 未结 4 2446
温柔的废话
温柔的废话 2021-02-20 16:53

Say that I have this data frame:

     1   2   3   4      
100  8   12  5   14 
99   1   6   4   3   
98   2   5   4   11  
97   5   3   7   2   
<
4条回答
  •  醉酒成梦
    2021-02-20 17:30

    Another aggregate variation, avoiding the formula interface, which actually complicates matters in this instance:

    aggregate(list(Sum=unlist(dat)), list(Group=LETTERS[c(row(dat) + col(dat))-1]), FUN=sum)
    
    #  Group Sum
    #1     A   8
    #2     B  13
    #3     C  13
    #4     D  28
    #5     E  10
    #6     F  18
    #7     G   2
    

提交回复
热议问题