which()takes an argument arr.ind=TRUEthat returns the indices of all the elements TRUEin the logical matrix to which it is applied.
#
set.seed(1)
m <- matrix(sample(1:100, 10), ncol=2)
m
# [,1] [,2]
# [1,] 27 86
# [2,] 37 97
# [3,] 57 62
# [4,] 89 58
# [5,] 20 6
#
which(m==58, arr.ind=TRUE)
# row col
# [1,] 4 2
source
share