How to sum over diagonals of data frame

后端 未结 4 2400
温柔的废话
温柔的废话 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:15

    Another solution using bgoldst's definition of df1 and df2

    sapply(unique(c(as.matrix(df2))),function(x) sum(df1[df2==x]))
    

    Gives

    #A  B  C  D  E  F  G 
    #8 13 13 28 10 18  2 
    

    (Not quite the format that you wanted, but maybe it's ok...)

提交回复
热议问题