简体   繁体   English

提取行和列的最大值

[英]Extract the max values for row and column

I need to fin the values for which the row max and the column max are for the same position. 我需要查找行最大和列最大用于相同位置的值。 Test data (The real data doesn't need to be a square matrix): 测试数据(实际数据不必是方矩阵):

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")))

I already found which are the columns/rows with the max value for that row/column. 我已经发现哪些列/行具有该行/列的最大值。

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"))

But I don't manage to get the values from the matrix. 但是我无法从矩阵中获取值。 The problem are when the same (max) value appear twice or more. 问题是当相同的(最大值)值出现两次或两次以上。 I don't know how to check the indices of that case. 我不知道如何检查该案件的索引。 I tried using mapply: 我尝试使用mapply:

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

But this stops when rows or cols has more than one element. 但这在行或列具有多个元素时停止。

Expected output: c(0.6, 0.4) 预期输出: c(0.6, 0.4)
The first is the max value of column 1 and row 2, the second value is the max value of row 1 and column 2. 第一个是第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

As you can see for row 2 and column 1 the max value is the same, and for row 1 and column 1 it is the same value, but for row 3 and column 3 it isn't 如您所见,第2行和第1列的最大值相同,而第1行和第1列的最大值相同,但第3行和第3列的最大值不同

I think , I understood what you are trying to do. 我想,我了解您正在尝试做的事情。 Not an optimal solution though. 虽然不是最佳解决方案。

We find out the indices for maximum value in rows as well as column and then find out the indices which intersect and display the corresponding value from the dataframe. 我们找出行和列中最大值的索引,然后找出与数据框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

And in one-line 并在一行中

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

This is what you want: 这就是你想要的:

# 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]

It also work with same max values on multiple rows/columns, for example with this data: 它还可以在多个行/列上使用相同的最大值,例如使用以下数据:

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