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):
我需要查找行max和列max对于相同位置的值。测试数据(真实数据不需要是方阵):
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)
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.
预期输出: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
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列,它不是
2 个解决方案
#1
2
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.
我们找出行和列中最大值的索引,然后找出相交的索引并显示数据帧中的相应值。
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))))]
#2
2
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")))
#1
2
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.
我们找出行和列中最大值的索引,然后找出相交的索引并显示数据帧中的相应值。
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))))]
#2
2
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")))