Index of item in matrix
-----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of John Janmaat Sent: Wednesday, April 02, 2003 12:39 PM To: r-help at stat.math.ethz.ch Subject: [R] Index of item in matrix Hello All, Is there a fast way to find the index(row and column) of a point in a
matrix? Thanks, John.
Look at ?which. Specifically the form: which(x, arr.ind = TRUE)
m <- matrix(1:12,3,4) m
[,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12
which(m == 9, arr.ind = TRUE)
row col [1,] 3 3
m[3,3]
[1] 9 HTH, Marc Schwartz