Re: [R] Fast way of finding top-n values of a long vector

2009-06-04 Thread Allan Engelhardt
Greg Snow wrote: Try adding a version that uses sort with the partial argument, that should be faster than regular sort (for long enough test vectors) and possibly faster than the max solutions when finding more than just the largest 2. I find the documentation for the partial argument in sor

Re: [R] Fast way of finding top-n values of a long vector

2009-06-04 Thread Greg Snow
e 04, 2009 2:18 AM > To: r-help@r-project.org > Subject: [R] Fast way of finding top-n values of a long vector > > If x is a (long) vector and n << length(x), what is a fast way of > finding the top-n values of x? > > Some suggestions (calculating the ratio of the two t

Re: [R] Fast way of finding top-n values of a long vector

2009-06-04 Thread Barry Rowlingson
On Thu, Jun 4, 2009 at 9:18 AM, Allan Engelhardt wrote: > I want to apply this code to a few tens of thousands of vectors so speed > does matter.  In C or similar I would of course calculate the result with a > single pass through x, and not with three passes as in 'max2'. > So why not code it

[R] Fast way of finding top-n values of a long vector

2009-06-04 Thread Allan Engelhardt
If x is a (long) vector and n << length(x), what is a fast way of finding the top-n values of x? Some suggestions (calculating the ratio of the two top values): library("rbenchmark") set.seed(1); x <- runif(1e6, max=1e7); x[1] <- NA; benchmark( replications=20, columns=c("test","elapsed"), ord