Skip to content
Prev 388627 / 398532 Next

Puzzled over "partial"

In this context, "partial" is not the name of any function or package in R.
It is just the name of a parameter.  And its meaning, which is specific to
sort(), is spelled out in the documentation for sort:
...
     If ?partial? is not ?NULL?, it is taken to contain indices of
     elements of the result which are to be placed in their correct
     positions in the sorted array by partial sorting.  For each of the
     result values in a specified position, any values smaller than
     that one are guaranteed to have a smaller index in the sorted
     array and any values which are greater are guaranteed to have a
     bigger index in the sorted array.  (This is included for
     efficiency, and many of the options are not available for partial
     sorting.  It is only substantially more efficient if ?partial? has
     a handful of elements, and a full sort is done (a Quicksort if
     possible) if there are more than 10.)  Names are discarded for
     partial sorting.

 Suppose you had the question
    IF the vector x were sorted, what would its 5th element be?
You could answer that by doing
    sort(x)[5[
but that's more work than you really need.
    sort(x, partial=5)[5]
rearranges x to c(less.than.x.5, x.5, greater.than.x.5).  This is called
partial sorting,   If you wanted the quartiles, then
   sort(x, partial=c(p,q,r))[c(p,q,r)]
will do the job, where p, q, r are the positions where the quartiles
would end up if complete sorting were done.

It's an efficiency hack for computing quantiles, basically.
If you are interested, you could look up
https://en.wikipedia.org/wiki/Quickselect
On Mon, 26 Jul 2021 at 23:54, Nick Wray <nickmwray at gmail.com> wrote: