The Complete Guide: How to Use sort(), order(), and rank() in R


Three functions in R that people often get confused are sort, order, and rank.

Here’s the difference between these functions:

  • sort() will sort a vector in ascending order
  • order() will return the index of each element in a vector in sorted order
  • rank() will assign a rank to each element in a vector (smallest = 1)

The following example shows how to use each of these functions in practice.

Example: Use sort(), order(), & rank() with Vectors

The following code shows how to use sort(), order(), and rank() functions with a vector with four values:

#create vector
x <- c(0, 20, 10, 15)

#sort vector
sort(x)

[1]  0 10 15 20

#order vector
order(x)

[1] 1 3 4 2

#rank vector
rank(x)

[1] 1 4 2 3

Here’s what each function did:

1. The sort() function simply sorted the values in the vector in ascending order.

2. The order() function returned the index of each element in sorted order.

  • If you put the values from the original vector in order based on these index values, you’ll end up with a sorted vector.
  • For example, order() tells us to put the value in index position 1 first – this is 0 in the original vector.
  • Then order() tells us to put the value in index position 3 next – this is 10 in the original vector.
  • Then order() tells us to put the value in index position 4 next – this is 15 in the original vector.
  • Then order() tells us to put the value in index position 2 next – this is 20 in the original vector.
  • The end result is a sorted vector – 0, 10, 15, 20.

3. The rank() function assigned a rank to each element in the vector, using 1 for the smallest value.

  • For example, rank() tells us that the first value in the original vector is the smallest (rank = 1) and the second value in the original vector is the largest (rank = 4)

Note that we can use the following syntax to use sort(), order(), and rank() in reverse order:

#create vector
x <- c(0, 20, 10, 15)

#sort vector in decreasing order
sort(x, decreasing=TRUE)

[1] 20 15 10  0

#order vector in decreasing order
order(x, decreasing=TRUE)

[1] 2 4 3 1

#rank vector in reverse order (largest value = 1)
rank(-x)

[1] 4 1 3 2

Notice that these results are the exact opposite of the ones produced in the previous examples.

Note: How to Handle Ties with rank() Function

We can use the ties.method argument to specify how we should handle ties when using the rank() function:

rank(x, ties.method='average')

You can use one of the following options to specify how to handle ties:

  • average: (Default) Assigns each tied element to the average rank (elements ranked in the 3rd and 4th position would both receive a rank of 3.5)
  • first: Assigns the first tied element to the lowest rank (elements ranked in the 3rd and 4th positions would receive ranks 3 and 4 respectively)
  • min: Assigns every tied element to the lowest rank (elements ranked in the 3rd and 4th position would both receive a rank of 3)
  • max: Assigns every tied element to the highest rank (elements ranked in the 3rd and 4th position would both receive a rank of 4)
  • random: Assigns every tied element to a random rank (either element tied for the 3rd and 4th position could receive either rank)

Depending on your scenario, one of these methods might make more sense to use than the others.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Sort by Multiple Columns in R
How to Sort a Data Frame by Date in R
How to Calculate Percentile Rank in R

Leave a Reply

Your email address will not be published.