Skip to content

Unexpected results from sort function when partial and index are used

3 messages · Tuszynski, Jaroslaw W., Sundar Dorai-Raj, Brian Ripley

#
Tuszynski, Jaroslaw W. wrote:

            
Jarek,

Looking at the code for sort, we see the following:

     if (!is.null(partial)) {
         if (!all(is.finite(partial)))
             stop("non-finite `partial'")
         y <- .Internal(psort(x, partial))
     } else {
        # other sort code
     }

so index.return is ignored if partial is provided. To get the index you 
can use ?match:

z <- rnorm(10)
x <- sort(z, partial = 3)
ix <- match(z, x)

Hopefully, I used ?match correctly. Please verify on your own.

--sundar
#
On Wed, 3 Nov 2004, Tuszynski, Jaroslaw W. wrote:

            
You cannot.  There is no underlying code to do so, and the person who 
added 'index.return' forgot this case.  I was against having it at all -- 
we have sort.list for that purpose.  I've updated the documentation.

Sundar's match() solution will not work if there are duplicate values.  
If you need the index, just do a full sort -- partial sorting is only 
implemented for efficiency reasons, and nowadays full sorting is fast 
enough even on massive vectors.