[R] which.max2()

0 views
Skip to first unread message

Esmail Bonakdarian

unread,
May 9, 2008, 9:07:44 AM5/9/08
to r-h...@r-project.org
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-h...@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.

Dimitris Rizopoulos

unread,
May 9, 2008, 9:23:38 AM5/9/08
to Esmail Bonakdarian, r-h...@r-project.org
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


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

Marc Schwartz

unread,
May 9, 2008, 11:11:15 AM5/9/08
to Esmail Bonakdarian, r-h...@r-project.org
on 05/09/2008 08:07 AM Esmail Bonakdarian wrote:
> 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

I might be tempted to take a more generic approach, where one can
provide an argument to the function to indicate that I want the 'top x'
maximum values and to give the user the option of returning the indices
or the values themselves.

Perhaps:

which.max2 <- function(x, top = 1, values = FALSE)
{
if (values)
rev(sort(x))[1:top]
else
order(x, decreasing = TRUE)[1:top]
}

set.seed(1)
Vec <- rnorm(10)

> Vec
[1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684
[7] 0.4874291 0.7383247 0.5757814 -0.3053884


> which.max2(Vec, 2)
[1] 4 8

> which.max2(Vec, 2, values = TRUE)
[1] 1.5952808 0.7383247


HTH,

Marc Schwartz

Esmail Bonakdarian

unread,
May 9, 2008, 1:24:25 PM5/9/08
to Dimitris Rizopoulos, r-h...@r-project.org
Dimitris Rizopoulos wrote:
> try this:
>
> v <- rnorm(10)
> v
> order(v, decreasing = TRUE)[1:2]

Wow .. that is slick! First I thought, wait .. I don't want to
reorder the elements, but this doesn't - it just returns the index
values in order. I don't really get that from reading the documentation,
it's probably there, but not that clear to me.

Thanks for showing me something more "R'ish".

Esmail Bonakdarian

unread,
May 9, 2008, 1:31:54 PM5/9/08
to marc_s...@comcast.net, r-h...@r-project.org
Marc Schwartz wrote:
>
> I might be tempted to take a more generic approach, where one can
> provide an argument to the function to indicate that I want the 'top x'
> maximum values and to give the user the option of returning the indices
> or the values themselves.
>
> Perhaps:
>
> which.max2 <- function(x, top = 1, values = FALSE)
> {
> if (values)
> rev(sort(x))[1:top]
> else
> order(x, decreasing = TRUE)[1:top]
> }

Very cool too! .. Thanks Marc. Again, I did not get this from the
order documentation (ie that it manipulate index values rather than
the values themselves). Great to see examples.

Best,
Esmail

Reply all
Reply to author
Forward
0 new messages