# [R] Question about base::rank results

Giorgio Garziano giorgio.garziano at ericsson.com
Mon Apr 27 13:38:38 CEST 2015

```Ok. Thanks for your explanation.

Cheers,
Giorgio Garziano

-----Original Message-----
From: Rolf Turner [mailto:r.turner at auckland.ac.nz]
Sent: lunedì 27 aprile 2015 09:24
To: Giorgio Garziano; r-help at r-project.org
Subject: Re: [R] Question about base::rank results

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?

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

```