[R] Partial sort?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jan 15 00:22:16 CET 2009


1) Unless you know what you are doing, you don't want to use it since 
the speed advantage is small at best.

2) Suppose you want the median of 2n+1 values.  Then all you need is a 
value with n others to the left and n to the right.  That is a 
partially sorted vector.  Example

> set.seed(123)
> x <- round(runif(11), 3)
> sort.int(x, partial=6)
  [1] 0.046 0.288 0.409 0.457 0.528 0.551 0.788 0.892 0.940 0.883 0.957

Thare are 5 values on each side of 0.551, but the ones on the right 
are not sorted.

Now timings:

set.seed(123)
system.time(for(i in 1:1e5) sort.int(round(runif(11), 3)))
set.seed(123)
system.time(for(i in 1:1e5) sort.int(round(runif(11), 3), partial=6))

Try it for yourself, but remember this is a normally a tiny part of a 
real problem and that specifying even 10% of the quantiles can be 
slower than a full sort.

On Wed, 14 Jan 2009, rkevinburton at charter.net wrote:

> This is definitely a newbie question but from the documentation I have not been able to figure out what the partial sort option on the sort method does. I have read and re-read the documentation and looked at the examples but for some reason it doesn't register. Would someone attempt to explain what sort with a non-null partial array of indices does?
>
> Thank you.
>
> Kevin
>
> ______________________________________________
> R-help at 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list