[R] Question about base::rank results
Rolf Turner
r.turner at auckland.ac.nz
Mon Apr 27 09:24:01 CEST 2015
On 26/04/15 20:17, Giorgio Garziano wrote:
> Hi,
>
> I cannot understand why rank(x) behaves as outlined below. Based on
> the results of first x vector values ranking, which is as expected in
> my opinion, I cannot explain the following results.
>
>> x <- c(12,34,15,77,78)
>> x[rank(x)]
> [1] 12 15 34 77 78 (OK)
>
>> x <- c(12,34,15,77,78,22)
>> x[rank(x)]
> [1] 12 77 34 78 22 15 (?)
>
>> x <- c(12,34,77,15,78)
>> x[rank(x)]
> [1] 12 77 15 34 78 (?)
>
> Please any feedback ? Thanks.
What did you expect to get?
To take your 2nd example:
x <- c(12,34,15,77,78,22)
x[rank(x)]
[1] 12 77 34 78 22 15 (?)
Why (?) ?
The rank of 12 is 1.
The rank of 34 is 4.
The rank of 15 is 2.
The rank of 77 is 5.
The rank of 78 is 6.
The rank of 22 is 3.
Thus x[rank(x)] gives you the 1st, 4th, 2nd, 5th, 6th and 3rd
entries of x. In that order.
What on earth is puzzling you?
I can think of no good reason for ever looking at x[rank(x)].
Perhaps, judging from your first example which you say is "OK",
you want x[order(x)].
cheers,
Rolf Turner
--
Rolf Turner
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
Home phone: +64-9-480-4619
More information about the R-help
mailing list