[R] [newbie] return index(s) for value?

Tom Roche Tom_Roche at pobox.com
Sun May 12 00:35:50 CEST 2013

Is there a function 'foo' such that, given an array and a value, iff
the value is present in the array, it returns the index(s) of the
value? E.g.,

> matrix(1:9, nrow=3) -> grid
> grid
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> foo(grid, median(grid))
[1] c(2,2)

I'm sure I could code this with nested loops, but am nearly as sure
that this functionality must already exist in R ... since there's so
much functionality already in R !-)

Apologies if this is a FAQ, but a fair amount of googling via
rseek.org is not finding an answer (probably because I'm not using the
correct search terms). Feel free (in fact, be encouraged :-) to reply
directly to me as well as to the list (I'm on the digest, which gets

TIA, Tom Roche <Tom_Roche at pobox.com>

More information about the R-help mailing list