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

Tom Roche Tom_Roche at pobox.com
Sun May 12 01:30:53 CEST 2013


https://stat.ethz.ch/pipermail/r-help/2013-May/353343.html
>> 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)

https://stat.ethz.ch/pipermail/r-help/2013-May/353344.html
> just use the argument arr.ind of ?which:

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

> Note that it defaults to FALSE.

and the result is a matrix. muito obrigado!



More information about the R-help mailing list