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
Specifying a partial sort allows you to specify a re-arrangement of the data into groups where all the values in any group are guaranteed to be at least as large as any value in the previous group. Within the groups themselves, though, the values are not sorted. Here is an example:> x <- round(runif(20), 2) > x[1] 0.18 0.23 0.03 0.97 0.41 0.87 0.79 0.16 0.11 0.85 0.96 0.74 0.56 [14] 0.34 0.75 0.03 0.34 0.39 0.01 0.71> x <- sort(x, partial = seq(5, 20, 5))> matrix(x, nrow=5)[,1] [,2] [,3] [,4] [1,] 0.01 0.18 0.71 0.79 [2,] 0.11 0.23 0.74 0.96 [3,] 0.03 0.34 0.56 0.85 [4,] 0.03 0.34 0.41 0.87 [5,] 0.16 0.39 0.75 0.97>Notice that the first five values (first column) are the 5 lowest, the next 5 are the next lowest, and so on. (Notice also that in this example the last group consists of the 20th value by itself, which is therefore the maximum for the entire vector in the partially sorted version.) Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of rkevinburton at charter.net Sent: Thursday, 15 January 2009 8:59 AM To: r-help at r-project.org Subject: [R] Partial sort? 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.
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
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? > >It guarantees that those particular indices are sorted correctly, but doesn't guarantee anything else. For example, > x <- 10:1 > sort(x, partial=1) guarantees that the first entry in x (i.e. 10) is placed correctly, but nothing else, and the result is: [1] 1 9 8 7 6 5 4 3 2 10 Duncan Murdoch