繁体   English   中英

提取行和列的最大值

[英]Extract the max values for row and column

我需要查找行最大和列最大用于相同位置的值。 测试数据(实际数据不必是方矩阵):

scores<-structure(c(0.4, 0.6, 0.222222222222222, 0.4, 0.4, 0, 0.25, 0.5, 
0.285714285714286), .Dim = c(3L, 3L), .Dimnames = list(c("a", 
"b", "c"), c("d", "e", "f")))

我已经发现哪些列/行具有该行/列的最大值。

rows<-structure(list(a = c("d", "e"), b = "d", c = "f"), .Names = c("a", 
"b", "c"))
cols<-structure(list(d = "b", e = c("a", "b"), f = "b"), .Names = c("d", 
"e", "f"))

但是我无法从矩阵中获取值。 问题是当相同的(最大值)值出现两次或两次以上。 我不知道如何检查该案件的索引。 我尝试使用mapply:

mapply(function(x, y) {
    cols[x] == rows[y] 
    }, rows, cols)

但这在行或列具有多个元素时停止。

预期输出: c(0.6, 0.4)
第一个是第1列和第2列的最大值,第二个是第1行和第2列的最大值。

            d   e         f   | Max
a   0.4000000 0.4 0.2500000   0.4
b   0.6000000 0.4 0.5000000   0.6
c   0.2222222 0.0 0.2857143   0.2857
Max:  0.6     0.4 0.5

如您所见,第2行和第1列的最大值相同,而第1行和第1列的最大值相同,但第3行和第3列的最大值不同

我想,我了解您正在尝试做的事情。 虽然不是最佳解决方案。

我们找出行和列中最大值的索引,然后找出与数据框intersect并显示相应值的索引。

a1 <- which(apply(scores, 1, function(x) x == max(x)))
a2 <- which(apply(scores, 2, function(x) x == max(x)))
scores[intersect(a1, a2)]

#[1] 0.6 0.4

并在一行中

scores[intersect(which(apply(scores, 1, function(x) x == max(x))), 
                 which(apply(scores, 2, function(x) x == max(x))))]

这就是你想要的:

# Compute rows and columns max and rows max positions
row_max<-apply(scores, 1, max)
row_max_pos<-apply(scores, 1, which.max)
col_max<-apply(scores, 2, max)

# For each row, check if max is equal to corresponding column max
res <- sapply(1:length(row_max), 
              function(i) ifelse(row_max[i] == col_max[row_max_pos[i]], T, F))
row_max[res]

它还可以在多个行/列上使用相同的最大值,例如使用以下数据:

scores <- structure(c(0.4, 0.6, 0.222222222222222, 0.4, 0.4, 0, 0.25, 0.5, 
                      0.285714285714286, 0.13, 0.2, 0.6), .Dim = c(4L, 3L), 
                      .Dimnames = list(c("a", "b", "c", "d"), c("e", "f", "g")))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM