Re: [R] I don't understand the 'order' function
There is a blog post about this: http://www.portfolioprobe.com/2012/07/26/r-inferno-ism-order-is-not-rank/ And proof that it is possible to confuse them even when you know the difference. Pat On 16/04/2013 19:10, Julio Sergio wrote: Julio Sergio juliosergio at gmail.com writes: I thought I've understood the 'order' function, using simple examples like: Thanks to you all!... As Sarah said, what was damaged was my understanding ( ;-) )... and as Duncan said, I was confusing 'order' with 'rank', thanks! Now I understand the 'order' function. -Sergio __ R-help@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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming') __ R-help@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.
Re: [R] I don't understand the 'order' function
On Apr 17, 2013, at 10:41 , Patrick Burns wrote: There is a blog post about this: http://www.portfolioprobe.com/2012/07/26/r-inferno-ism-order-is-not-rank/ And proof that it is possible to confuse them even when you know the difference. It usually helps to remember that x[order(x)] is sort(x) (and that x[rank(x)] is nothing of the sort). It's somewhat elusive, but not impossible to realize that the two are inverses (if no ties). Duncan M. indicated it nicely earlier in the thread: rank() is how to permute ordered data to get those observed, order is how to permute the data to put them in order. They are inverses in terms of composition of permutations, not as transformations of sets of integers: rank(order(x)) and order(rank(x)) are both equal to order(x), whereas x - rnorm(5) rank(x) [1] 4 3 5 2 1 order(x) [1] 5 4 2 1 3 ## permutation matrix P - matrix(0,5,5); diag(P[,order(x)]) - 1 P %*% 1:5 [,1] [1,]5 [2,]4 [3,]2 [4,]1 [5,]3 P2 - matrix(0,5,5); diag(P2[,rank(x)]) - 1 P2 %*% 1:5 [,1] [1,]4 [2,]3 [3,]5 [4,]2 [5,]1 P %*% P2 [,1] [,2] [,3] [,4] [,5] [1,]10000 [2,]01000 [3,]00100 [4,]00010 [5,]00001 Or, as Duncan put it: rank(x)[order(x)] and order(x)[rank(x)] are 1:length(x). The thing that tends to blow my mind is that order(order(x))==rank(x). I can't seem to get my intuition to fathom it, although there's a fairly easy proof in that 1:N == sort(order(x)) == order(x)[order(order(x))] == order(x)[rank(x)] -- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.
[R] I don't understand the 'order' function
I thought I've understood the 'order' function, using simple examples like: order(c(5,4,-2)) [1] 3 2 1 However, I arrived to the following example: order(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 8 9 10 7 11 6 5 4 3 2 1 and I was completely perplexed! Shouldn't the output vector be 11 10 9 8 7 6 4 1 2 3 5 ? Do I have a damaged version of R? I became still more astonished when I used the sort function and got the right answer: sort(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 210 210 505 920 1045 1210 1335 1545 2085 2255 2465 since 'sort' documentation claims to be using 'order' to establish the right order. Please help me to understand all this! Thanks, -Sergio. __ R-help@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.
Re: [R] I don't understand the 'order' function
Hi Julio, On Tue, Apr 16, 2013 at 1:51 PM, Julio Sergio julioser...@gmail.com wrote: I thought I've understood the 'order' function, using simple examples like: order(c(5,4,-2)) [1] 3 2 1 However, I arrived to the following example: order(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 8 9 10 7 11 6 5 4 3 2 1 and I was completely perplexed! Shouldn't the output vector be 11 10 9 8 7 6 4 1 2 3 5 ? Do I have a damaged version of R? Your version of R is fine; your understanding is damaged. :) order() returns the element indices for each position. So in your example, the sorted version of the vector would have element 8 in the first place, element 9 in the second place, and element 1 in the last place. order() is not the same as rank(). See: x - c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045) order(x) x[order(x)] rank(x) # what you seem to expect Sarah -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
Re: [R] I don't understand the 'order' function
Hello, Inline. Em 16-04-2013 18:51, Julio Sergio escreveu: I thought I've understood the 'order' function, using simple examples like: order(c(5,4,-2)) [1] 3 2 1 However, I arrived to the following example: order(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 8 9 10 7 11 6 5 4 3 2 1 and I was completely perplexed! Shouldn't the output vector be 11 10 9 8 7 6 4 1 2 3 5 ? No, why should it? Try assigning the output of order and see what happens to the vector. x - c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045) (o - order(x) ) x[o] # Allright Hope this helps, Rui Barradas Do I have a damaged version of R? I became still more astonished when I used the sort function and got the right answer: sort(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 210 210 505 920 1045 1210 1335 1545 2085 2255 2465 since 'sort' documentation claims to be using 'order' to establish the right order. Please help me to understand all this! Thanks, -Sergio. __ R-help@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. __ R-help@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.
Re: [R] I don't understand the 'order' function
Hi, vec1- c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045) vec1[order(vec1)] #[1] 210 210 505 920 1045 1210 1335 1545 2085 2255 2465 order(vec1) #[1] 8 9 10 7 11 6 5 4 3 2 1 sort(vec1,index.return=TRUE) #$x #[1] 210 210 505 920 1045 1210 1335 1545 2085 2255 2465 #$ix # [1] 8 9 10 7 11 6 5 4 3 2 1 A.K. - Original Message - From: Julio Sergio julioser...@gmail.com To: r-h...@stat.math.ethz.ch Cc: Sent: Tuesday, April 16, 2013 1:51 PM Subject: [R] I don't understand the 'order' function I thought I've understood the 'order' function, using simple examples like: order(c(5,4,-2)) [1] 3 2 1 However, I arrived to the following example: order(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 8 9 10 7 11 6 5 4 3 2 1 and I was completely perplexed! Shouldn't the output vector be 11 10 9 8 7 6 4 1 2 3 5 ? Do I have a damaged version of R? I became still more astonished when I used the sort function and got the right answer: sort(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 210 210 505 920 1045 1210 1335 1545 2085 2255 2465 since 'sort' documentation claims to be using 'order' to establish the right order. Please help me to understand all this! Thanks, -Sergio. __ R-help@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. __ R-help@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.
Re: [R] I don't understand the 'order' function
On 16/04/2013 1:51 PM, Julio Sergio wrote: I thought I've understood the 'order' function, using simple examples like: order(c(5,4,-2)) [1] 3 2 1 However, I arrived to the following example: order(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 8 9 10 7 11 6 5 4 3 2 1 and I was completely perplexed! Shouldn't the output vector be 11 10 9 8 7 6 4 1 2 3 5 ? Do I have a damaged version of R? You are probably confusing order() and rank(). What we want is that x[order(x)] is in increasing order. This is the inverse permutation of what rank(x) gives, so (if there are no ties) rank(x)[order(x)] and order(x)[rank(x)] should both give 1:length(x). Duncan I became still more astonished when I used the sort function and got the right answer: sort(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 210 210 505 920 1045 1210 1335 1545 2085 2255 2465 since 'sort' documentation claims to be using 'order' to establish the right order. Please help me to understand all this! Thanks, -Sergio. __ R-help@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. __ R-help@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.
Re: [R] I don't understand the 'order' function
Julio Sergio juliosergio at gmail.com writes: I thought I've understood the 'order' function, using simple examples like: Thanks to you all!... As Sarah said, what was damaged was my understanding ( ;-) )... and as Duncan said, I was confusing 'order' with 'rank', thanks! Now I understand the 'order' function. -Sergio __ R-help@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.
Re: [R] I don't understand the 'order' function
[See in-line below[ On 16-Apr-2013 17:51:41 Julio Sergio wrote: I thought I've understood the 'order' function, using simple examples like: order(c(5,4,-2)) [1] 3 2 1 However, I arrived to the following example: order(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 8 9 10 7 11 6 5 4 3 2 1 and I was completely perplexed! Shouldn't the output vector be 11 10 9 8 7 6 4 1 2 3 5 ? Do I have a damaged version of R? I think the simplest explanation can be given as: S - c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045) cbind(Index=1:length(S), S, Order=order(S), Sort=sort(S)) IndexS Order Sort [1,] 1 2465 8 210 [2,] 2 2255 9 210 [3,] 3 208510 505 [4,] 4 1545 7 920 [5,] 5 133511 1045 [6,] 6 1210 6 1210 [7,] 7 920 5 1335 [8,] 8 210 4 1545 [9,] 9 210 3 2085 [10,]10 505 2 2255 [11,]11 1045 1 2465 showing that the value of 'order' for any one of the numbers is the Index (position) of that number in the original series, placed in the position that number occupies in the sorted series. (With a tie for S[8] = S[9] = 210). For example: which one of S occurs in 5th position in the sorted series? It is the 11th of S (1045). I became still more astonished when I used the sort function and got the right answer: sort(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)) [1] 210 210 505 920 1045 1210 1335 1545 2085 2255 2465 since 'sort' documentation claims to be using 'order' to establish the right order. Indeed, once you have order(S), you know which element of S to put in each position of the sorted order: S[order(S)] [1] 210 210 505 920 1045 1210 1335 1545 2085 2255 2465 Does this help to explain it? Ted. Please help me to understand all this! Thanks, -Sergio. __ R-help@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. - E-Mail: (Ted Harding) ted.hard...@wlandres.net Date: 16-Apr-2013 Time: 19:12:21 This message was sent by XFMail __ R-help@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.
Re: [R] I don't understand the 'order' function
I think Duncan said that order and rank were inverses (if there are no ties). order() has period 2 so order(order(x)) is also rank(x) if there are no ties. E.g., data.frame(x, o1=order(x), o2=order(order(x)), o3=order(order(order(x))), o4=order(order(order(order(x, rank=rank(x)) x o1 o2 o3 o4 rank 1 2465 8 11 8 11 11.0 2 2255 9 10 9 10 10.0 3 2085 10 9 10 9 9.0 4 1545 7 8 7 8 8.0 5 1335 11 7 11 7 7.0 6 1210 6 6 6 6 6.0 7 920 5 4 5 4 4.0 8 210 4 1 4 1 1.5 9 210 3 2 3 2 1.5 10 505 2 3 2 3 3.0 11 1045 1 5 1 5 5.0 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Julio Sergio Sent: Tuesday, April 16, 2013 11:10 AM To: r-h...@stat.math.ethz.ch Subject: Re: [R] I don't understand the 'order' function Julio Sergio juliosergio at gmail.com writes: I thought I've understood the 'order' function, using simple examples like: Thanks to you all!... As Sarah said, what was damaged was my understanding ( ;-) )... and as Duncan said, I was confusing 'order' with 'rank', thanks! Now I understand the 'order' function. -Sergio __ R-help@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. __ R-help@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.
Re: [R] I don't understand the 'order' function
William Dunlap wdunlap at tibco.com writes: I think Duncan said that order and rank were inverses (if there are no ties). order() has period 2 so order(order(x)) is also rank(x) if there are no ties. E.g., Thanks William! This is very interesting. So, applying order two times I can have a rank index for each element. Thanks, -Sergio. __ R-help@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.