[英]How to sum over diagonals of data frame
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.在上面的数据框中,这些值表示对
(100, 1), (99, 1)
等进行的观察次数的计数。
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 (ie, 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
例如,
D
是5+5+4+14
You can use row()
and col()
to identify row/column relationships.您可以使用
row()
和col()
来识别行/列关系。
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)或(如 ?@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 ... as.matrix()
需要使split()
正常工作......
Another aggregate
variation, avoiding the formula interface, which actually complicates matters in this instance:另一个
aggregate
变体,避免了公式接口,这在这种情况下实际上使问题复杂化:
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
Another solution using bgoldst's definition of df1
and df2
使用 bgoldst 对
df1
和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...) (不完全是您想要的格式,但也许没问题...)
Here's a solution using stack()
, and aggregate()
, although it requires the second data.frame contain character vectors, as opposed to factors (could be forced with lapply(df2,as.character)
):这是使用
stack()
和aggregate()
的解决方案,尽管它要求第二个 data.frame 包含字符向量,而不是因子(可以使用lapply(df2,as.character)
强制):
df1 <- data.frame(a=c(8,1,2,5), b=c(12,6,5,3), c=c(5,4,4,7), d=c(14,3,11,2) );
df2 <- data.frame(a=c('A','B','C','D'), b=c('B','C','D','E'), c=c('C','D','E','F'), d=c('D','E','F','G'), stringsAsFactors=F );
aggregate(sum~group,data.frame(sum=stack(df1)[,1],group=stack(df2)[,1]),sum);
## group sum
## 1 A 8
## 2 B 13
## 3 C 13
## 4 D 28
## 5 E 10
## 6 F 18
## 7 G 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.