给定以下矩阵假设我想在第二列中找到最大值:
mat <- matrix(c(1:3,7:9,4:6), byrow = T, nc = 3)
mat
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 7 8 9
[3,] 4 5 6
Run Code Online (Sandbox Code Playgroud)
我知道max(mat[,2])会返回8.如何返回行索引,在这种情况下第二行?
Dan*_*bić 164
看到 ?which.max
> which.max( matrix[,2] )
[1] 2
Run Code Online (Sandbox Code Playgroud)
yoy*_*sef 27
见?order.你只需要最后一个索引(或者首先是递减的顺序),所以这应该可以解决问题:
order(matrix[,2],decreasing=T)[1]
Run Code Online (Sandbox Code Playgroud)