[R] Finding indexes of minum and maximum elements of an array

Phil Spector spector at stat.berkeley.edu
Tue Dec 28 01:17:12 CET 2010


Here's one way:

> H = array(c(8:5,1:4),dim=c(2,2,2))
> which(H==min(H),arr.ind=TRUE)
      dim1 dim2 dim3
[1,]    1    1    2
> which(H==max(H),arr.ind=TRUE)
      dim1 dim2 dim3
[1,]    1    1    1
 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Mon, 27 Dec 2010, Eduardo de Oliveira Horta wrote:

> Hello there
>
> I wish to get the "coordinates" of the minimum element of an array.
>
> For example, if the array were
>
>> H = array(c(8:5,1:4),dim=c(2,2,2))
>> H
> , , 1
>
>     [,1] [,2]
> [1,]    8    6
> [2,]    7    5
>
> , , 2
>
>     [,1] [,2]
> [1,]    1    3
> [2,]    2    4
>
> then
>> min(H)
> [1] 1
>
> and
>> max(H)
> [1] 8
>
> Say "idx" were the function I'm looking for. Then, what I'm expecting is
>
>> idx(min(H))
> [1] 1 1 2
>> idx(max(H))
> [1] 1 1 1
>
> Thanks in advance,
>
> Eduardo de Oliveira Horta
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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