[R] which.max2()

Esmail Bonakdarian esmail.js at gmail.com
Fri May 9 15:07:44 CEST 2008


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



More information about the R-help mailing list