[R] which.max2()

Esmail Bonakdarian esmail.js at gmail.com
Fri May 9 19:31:54 CEST 2008


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



More information about the R-help mailing list