rank and order in R

rank is more complicated and not neccessarily an index (integer):

> rank(c(1))
[1] 1
> rank(c(1,1))
[1] 1.5 1.5
> rank(c(1,1,1))
[1] 2 2 2
> rank(c(1,1,1,1))
[1] 2.5 2.5 2.5 2.5

set.seed(1)
x <- sample(1:50, 30)    
x
# [1] 14 19 28 43 10 41 42 29 27  3  9  7 44 15 48 18 25 33 13 34 47 39 49  4 30 46  1 40 20  8
rank(x)
# [1]  9 12 16 25  7 23 24 17 15  2  6  4 26 10 29 11 14 19  8 20 28 21 30  3 18 27  1 22 13  5
order(x)
# [1] 27 10 24 12 30 11  5 19  1 14 16  2 29 17  9  3  8 25 18 20 22 28  6  7  4 13 26 21 15 23

rank returns a vector with the "rank" of each value. the number in the first position is the 9th lowest. order returns the indices that would put the initial vector x in order.

The 27th value of x is the lowest, so 27 is the first element of order(x) - and if you look at rank(x), the 27th element is 1.

x[order(x)]
# [1]  1  3  4  7  8  9 10 13 14 15 18 19 20 25 27 28 29 30 33 34 39 40 41 42 43 44 46 47 48 49

I always find it confusing to think about the difference between the two, and I always think, "how can I get to order using rank"?

Starting with Justin's example:

Order using rank:

## Setup example to match Justin's example
set.seed(1)
x <- sample(1:50, 30) 

## Make a vector to store the sorted x values
xx = integer(length(x))

## i is the index, ir is the ith "rank" value
i = 0
for(ir in rank(x)){
    i = i + 1
    xx[ir] = x[i]
}

all(xx==x[order(x)])
[1] TRUE

As it turned out this was a special case and made things confusing. I explain below for anyone interested:

rank returns the order of each element in an ascending list

order returns the index each element would have in an ascending list

Tags:

Sorting

R

R Faq