Re: [R] I don't know the difference between rank and order
On 21-Nov-2012 02:57:19 li1127217ye wrote: I don't know the difference between rank and order.For example: x=c(10,30,30,20,10,20) x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 x[order(x)] [1] 10 10 20 20 30 30 the result is quite different, x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 It is not sorted,why? It is because rank() gives, for each element of x, the position of that value within the sorted series of all the values in x. This will not, in general, be the same as the index, within x, of the value that should be in that position. Example: x1=c(6,5,4,2,3,1) x1[rank(x1,ties.method=first)] # [1] 1 3 2 5 4 6 rank(x1,ties.method=first) # [1] 6 5 4 2 3 1 So 2 indeed has rank 2, and 3 has rank 3; but what will be returned by x1[rank(x)] will depend on what is in x[2] and x[3] (in this case 5 and 4 respectively). Ted. - E-Mail: (Ted Harding) ted.hard...@wlandres.net Date: 21-Nov-2012 Time: 08:13:23 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 know the difference between rank and order
Obviously something that is possible to get wrong even when you know it: http://www.portfolioprobe.com/2012/07/26/r-inferno-ism-order-is-not-rank/ Pat On 21/11/2012 08:13, (Ted Harding) wrote: On 21-Nov-2012 02:57:19 li1127217ye wrote: I don't know the difference between rank and order.For example: x=c(10,30,30,20,10,20) x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 x[order(x)] [1] 10 10 20 20 30 30 the result is quite different, x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 It is not sorted,why? It is because rank() gives, for each element of x, the position of that value within the sorted series of all the values in x. This will not, in general, be the same as the index, within x, of the value that should be in that position. Example: x1=c(6,5,4,2,3,1) x1[rank(x1,ties.method=first)] # [1] 1 3 2 5 4 6 rank(x1,ties.method=first) # [1] 6 5 4 2 3 1 So 2 indeed has rank 2, and 3 has rank 3; but what will be returned by x1[rank(x)] will depend on what is in x[2] and x[3] (in this case 5 and 4 respectively). Ted. - E-Mail: (Ted Harding) ted.hard...@wlandres.net Date: 21-Nov-2012 Time: 08:13:23 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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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 know the difference between rank and order
On 12-11-21 4:59 AM, Patrick Burns wrote: Obviously something that is possible to get wrong even when you know it: http://www.portfolioprobe.com/2012/07/26/r-inferno-ism-order-is-not-rank/ They're not just different, they are inverses of each other: x - rnorm(10) rank(x) [1] 8 1 4 2 6 10 7 9 3 5 order(x) [1] 2 4 9 3 10 5 7 1 8 6 order(x)[rank(x)] [1] 1 2 3 4 5 6 7 8 9 10 rank(x)[order(x)] [1] 1 2 3 4 5 6 7 8 9 10 Duncan Murdoch Pat On 21/11/2012 08:13, (Ted Harding) wrote: On 21-Nov-2012 02:57:19 li1127217ye wrote: I don't know the difference between rank and order.For example: x=c(10,30,30,20,10,20) x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 x[order(x)] [1] 10 10 20 20 30 30 the result is quite different, x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 It is not sorted,why? It is because rank() gives, for each element of x, the position of that value within the sorted series of all the values in x. This will not, in general, be the same as the index, within x, of the value that should be in that position. Example: x1=c(6,5,4,2,3,1) x1[rank(x1,ties.method=first)] # [1] 1 3 2 5 4 6 rank(x1,ties.method=first) # [1] 6 5 4 2 3 1 So 2 indeed has rank 2, and 3 has rank 3; but what will be returned by x1[rank(x)] will depend on what is in x[2] and x[3] (in this case 5 and 4 respectively). Ted. - E-Mail: (Ted Harding) ted.hard...@wlandres.net Date: 21-Nov-2012 Time: 08:13:23 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. __ 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 know the difference between rank and order
I don't know the difference between rank and order.For example: x=c(10,30,30,20,10,20) x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 x[order(x)] [1] 10 10 20 20 30 30 the result is quite different, x[rank(x,ties.method=first)] [1] 10 10 20 30 30 20 It is not sorted,why? -- View this message in context: http://r.789695.n4.nabble.com/I-don-t-know-the-difference-between-rank-and-order-tp4650246.html Sent from the R help mailing list archive at Nabble.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.