Given matrix
x <- matrix(c(1,2,3,4), nrow=2, ncol=2)
colnames(x) <- c('a','b')
rownames(x) <- c('c','d')
How do I find the column index/name and row index/name of the minimum value?
I've tried which.min, but I need to get the row/column index rather than the element. Any ideas?
You can use which
which(x == min(x), arr.ind = TRUE)
For example :
x <- matrix(c(1, 2, 0, 4), nrow = 2, ncol = 2)
which(x == min(x), arr.ind = TRUE)
## row col
## [1,] 1 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With