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

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun May 12 00:48:56 CEST 2013



On 12.05.2013 00:35, Tom Roche wrote:
>
> 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)

which(grid==median(grid), arr.ind=TRUE)

Uwe Ligges

>
> 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
> huge).
>
> TIA, Tom Roche <Tom_Roche at pobox.com>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list