Re: [R] which.max2()

From: Esmail Bonakdarian <>
Date: Fri, 09 May 2008 13:31:54 -0400

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.

Esmail mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri 09 May 2008 - 18:13:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 09 May 2008 - 18:30:36 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive