如何对数据框的对角线求和 [英] How to sum over diagonals of data frame

查看:50
本文介绍了如何对数据框的对角线求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个数据框:

<代码> 1 2 3 4100 8 12 5 1499 1 6 4 398 2 5 4 1197 5 3 7 2

在上面的数据框中,这些值表示对 (100, 1), (99, 1) 等进行的观察次数的计数.

在我的上下文中,对角线具有相同的含义:

<代码> 1 2 3 4100 A B C D99 B C D E第 98 章97 D E F G

如何对第一个数据框中的对角线求和(即,对相似字母的计数求和)?

这将产生:

组总和一个 8乙 1313D 2810182

例如D就是5+5+4+14

解决方案

您可以使用 row()col() 来识别行/列关系.

>

m <- read.table(text="1 2 3 4100 8 12 5 1499 1 6 4 398 2 5 4 1197 5 3 7 2")vals <- sapply(2:8,函数(j) sum(m[row(m)+col(m)==j]))

或(如 ?@thelatemail 在评论中建议的那样)

vals <- sapply(split(as.matrix(m), row(m) + col(m)), sum)data.frame(group=LETTERS[seq_along(vals)],sum=vals)

或(@Frank)

data.frame(vals = tapply(as.matrix(m),(LETTERS[row(m) + col(m)-1]), sum))

as.matrix() 是使 split() 正常工作所必需的 ...

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   

In this above data frame, the values indicate counts of how many observations take on (100, 1), (99, 1), etc.

In my context, the diagonals have the same meanings:

     1   2   3   4
100  A   B   C   D 
99   B   C   D   E  
98   C   D   E   F 
97   D   E   F   G

How would I sum across the diagonals (i.e., sum the counts of the like letters) in the first data frame?

This would produce:

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

For example, D is 5+5+4+14

解决方案

You can use row() and col() to identify row/column relationships.

m <- read.table(text="
    1   2   3   4      
100  8   12  5   14 
99   1   6   4   3   
98   2   5   4   11  
97   5   3   7   2")

vals <- sapply(2:8,
       function(j) sum(m[row(m)+col(m)==j]))

or (as suggested in comments by ?@thelatemail)

vals <- sapply(split(as.matrix(m), row(m) + col(m)), sum)
data.frame(group=LETTERS[seq_along(vals)],sum=vals)

or (@Frank)

data.frame(vals = tapply(as.matrix(m), 
       (LETTERS[row(m) + col(m)-1]), sum))

as.matrix() is required to make split() work correctly ...

这篇关于如何对数据框的对角线求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆