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

Rui Barradas ruipbarradas at sapo.pt
Sun May 12 00:43:58 CEST 2013


Hello,

To that effect, just use the argument arr.ind of ?which:

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


Note that it defaults to FALSE.

Hope this helps,

Rui Barradas

Em 11-05-2013 23:35, Tom Roche escreveu:
>
> 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
> 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