Derar R-people I have troubles understanding what the function sort.list() is doing. On the homepage it says that it returns a permutation which rearranges a vector into ascending or descending order (like order() but on a vector instead of a sequence).> sort.list(c(0, 2, 10, 11, 4))[1] 1 2 5 3 4 which does not make sense to me. In fact I am getting the same (non-sensical) result using order() order(c(0, 2, 10, 11, 4)) [1] 1 2 5 3 4 Of course rank(c(0, 2, 10, 11, 4)) 1] 1 2 4 5 3 gives the correct result. Am I missing something obvious here ? Cheers Ole -- Ole F. Christensen Department of Mathematics and Statistics Fylde College, Lancaster University Lancaster, LA1 4YF, England -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ole Christensen wrote:> > Derar R-people > > I have troubles understanding what the function sort.list() is doing. On > the homepage it says that it returns a permutation which rearranges a > vector into ascending or descending order (like order() but on a vector > instead of a sequence). > > > sort.list(c(0, 2, 10, 11, 4)) > [1] 1 2 5 3 4 > > which does not make sense to me. > > In fact I am getting the same (non-sensical) result using order() > > order(c(0, 2, 10, 11, 4)) > [1] 1 2 5 3 4 > > Of course > > rank(c(0, 2, 10, 11, 4)) > 1] 1 2 4 5 3 > > gives the correct result.Both functions give correct results, they just do different things! rank() gives you the ranks, order() returns what is sometimes also called anti-ranks. Type R> c(0, 2, 10, 11, 4)[order(c(0, 2, 10, 11, 4))] to see what it does. It can be used to rearrange matrices for example. See help(order) for further examples. Z> Am I missing something obvious here ? > > Cheers Ole > > -- > Ole F. Christensen > Department of Mathematics and Statistics > Fylde College, Lancaster University > Lancaster, LA1 4YF, England > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, May 06, 2002 at 10:50:11AM +0100, Ole Christensen wrote:> I have troubles understanding what the function sort.list() is doing....> > sort.list(c(0, 2, 10, 11, 4)) > [1] 1 2 5 3 4...> order(c(0, 2, 10, 11, 4)) > [1] 1 2 5 3 4 > Am I missing something obvious here ?Yup. It's doing exactly what it should. Try zz <- c(0, 2, 10, 11, 4) zz[order(zz)] and all will be revealed. order() returns indices of the vector if it were to be shuffled into order. I thought it was strange at first too, but when combined with R's other powerful scripting capabilities, it's a truly amazing workhorse function; don't let its size fool you ;) . Cheers Jason -- Indigo Industrial Controls Ltd. 64-21-343-545 jasont at indigoindustrial.co.nz -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Derar R-people > > I have troubles understanding what the function sort.list() is doing. On > the homepage it says that it returns a permutation which rearranges a > vector into ascending or descending order (like order() but on a vector > instead of a sequence). > > > sort.list(c(0, 2, 10, 11, 4)) > [1] 1 2 5 3 4 > > which does not make sense to me. > > In fact I am getting the same (non-sensical) result using order() > > order(c(0, 2, 10, 11, 4)) > [1] 1 2 5 3 4sort.list and order do return the correct order. What you're seeing is the indicies that will put the list in order. Try this to see> c(0,2,10,11,4)[sort.list(c(0,2,10,11,4))][1] 0 2 4 10 11> c(0,2,10,11,4)[order(c(0,2,10,11,4))][1] 0 2 4 10 11> > Of course > > rank(c(0, 2, 10, 11, 4)) > 1] 1 2 4 5 3 > > gives the correct result.Not "correct result" but "totally different result."> > Am I missing something obvious here ? >We all have those moments? Regards, Sundar -- Sundar Dorai-Raj, Ph.D. Statistical Methods Engineer PDF Solutions, Inc. Richardson TX (972) 889-3085 x216 (214) 392-7619 cell sundard at pdf.com -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._