[R] location of maximum

Agustin Lobo alobo at ija.csic.es
Tue Mar 5 19:35:49 CET 2002


For a vector:
> max(a1)
[1] 16667
> (1:length(a1))[a1==max(a1)]
[1] 6175
or
> which.max(a1)
[1] 6175

For a matrix:
> a2 <- matrix(1:12,ncol=3)
> a2
     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> max(a2)
[1] 12
> which.max(a2)
[1] 12
> col(a2)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    2    3
[3,]    1    2    3
[4,]    1    2    3
> col(a2)[12]
[1] 3
> row(a2)[12]
[1] 4

Agus


Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es


On Tue, 5 Mar 2002, Patrick Buetzberger wrote:

> Dear R-users,
> 
> I'm trying to extract the exact location of the maximum value in a
> vector or a matrix, meaning I want not only the maximum itself but also
> at which position (e.g row 6, column 12) it is located.
> I appreciate any suggestions.
> Thanks,
> 
> Patrick
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list