[R] which.max2()

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Fri May 9 15:23:38 CEST 2008


try this:

v <- rnorm(10)
v
order(v, decreasing = TRUE)[1:2]


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Esmail Bonakdarian" <esmail.js at gmail.com>
To: <r-help at r-project.org>
Sent: Friday, May 09, 2008 3:07 PM
Subject: [R] which.max2()


> Hello,
>
> which.max() only returns one index value, the one for the
> maximum value. If I want the two index values for the two
> largest values, is this a decent solution, or is there a
> nicer/better R'ish way?
>
> max2 <-function(v)
> {
>     m=which.max(v)
>     v[m] = -v[m]
>     m2=which.max(v)
>     result=c(m, m2)
>     result
> }
>
> Seems to work ok.
>
> Thanks,
> Esmail
>
> ______________________________________________
> 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list