Tuszynski, Jaroslaw W.
2004-Nov-03 22:41 UTC
[R] Unexpected results from sort function when partial and index are used
Hi, Consider the following example: sort(10:1, partial=3) ## 1 2 3 7 6 5 4 8 9 10 sort(10:1, index=T) ## $x: 1 2 3 4 5 6 7 8 9 10 ## $ix: 10 9 8 7 6 5 4 3 2 1 sort(10:1, partial=3, index=T) ## 1 2 3 7 6 5 4 8 9 10 The first 2 calls gave expected returns; however, the third one did not returned an index as requested. I could not find anything about it in http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html <http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html> , so it seems to be an "undocumented feature". Does any body know how to "convince" sort to return index of partially sorted array? Thanks Jarek =====================================\==== Jarek Tuszynski, PhD. o / \ Science Applications International Corporation <\__,| (703) 676-4192 "> \ Jaroslaw.W.Tuszynski@saic.com ` \ [[alternative HTML version deleted]]
Sundar Dorai-Raj
2004-Nov-04 00:13 UTC
[R] Unexpected results from sort function when partial and index are used
Tuszynski, Jaroslaw W. wrote:> Hi, > > Consider the following example: > > sort(10:1, partial=3) > ## 1 2 3 7 6 5 4 8 9 10 > > sort(10:1, index=T) > ## $x: 1 2 3 4 5 6 7 8 9 10 > ## $ix: 10 9 8 7 6 5 4 3 2 1 > > sort(10:1, partial=3, index=T) > ## 1 2 3 7 6 5 4 8 9 10 > > The first 2 calls gave expected returns; however, the third one did not > returned an index as requested. I could not find anything about it in > http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html > <http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html> , so it > seems to be an "undocumented feature". > > Does any body know how to "convince" sort to return index of partially > sorted array? > > Thanks > > JarekJarek, 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
Prof Brian Ripley
2004-Nov-04 07:34 UTC
[R] Unexpected results from sort function when partial and index are used
On Wed, 3 Nov 2004, Tuszynski, Jaroslaw W. wrote:> Hi, > > Consider the following example: > > sort(10:1, partial=3) > ## 1 2 3 7 6 5 4 8 9 10 > > sort(10:1, index=T) > ## $x: 1 2 3 4 5 6 7 8 9 10 > ## $ix: 10 9 8 7 6 5 4 3 2 1 > > sort(10:1, partial=3, index=T) > ## 1 2 3 7 6 5 4 8 9 10 > > The first 2 calls gave expected returns; however, the third one did not > returned an index as requested. I could not find anything about it in > http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html > <http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html> , so it > seems to be an "undocumented feature". > > Does any body know how to "convince" sort to return index of partially > sorted array?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. -- 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