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

______________________________________________
R-help@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.

Reply via email to