[R] Index of item in matrix

Marc Schwartz mschwartz at medanalytics.com
Thu Apr 3 04:17:07 CEST 2003


>-----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



More information about the R-help mailing list