You can use the **rank()** function in base R to return a rank of values in a vector.

This function uses the following basic syntax:

**rank(x, na.last=TRUE, ties.method=”average”)**

where:

**x**: The vector that contains the values to rank**na.last**: If TRUE, missing values are put last. If FALSE, missing values are put first.**ties.method**: How to handle ties (default is “average”)

The **ties.method** argument within the **rank()** function specifies how ties should be handled.

This argument accepts the following options:

**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)**last**: Assigns the last tied element to the lowest rank (elements ranked in the 3rd and 4th positions would receive ranks 4 and 3 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)

The following examples show how to use each option in practice with the following data frame in R:

#create data frame df <- data.frame(player=c('A', 'B', 'C', 'D', 'E'), points=c(5, 8, 10, 10, 17)) #view data frame df player points 1 A 5 2 B 8 3 C 10 4 D 10 5 E 17

**Example 1: Use rank() with ties.method=”average”**

The following code shows how to create a new column that uses **rank()** with **ties.method=”average”** to assign a rank to each player based on the value in the **points** column:

#create new column that ranks players based on their points value df$points_rank = rank(df$points, ties.method="average") #view updated data frame df player points points_rank 1 A 5 1.0 2 B 8 2.0 3 C 10 3.5 4 D 10 3.5 5 E 17 5.0

Since players C and D had the same number of points and were in rank positions 3 and 4, they both received the average of their rankings: **3.5**.

**Example 2: Use rank() with ties.method=”first”**

The following code shows how to create a new column that uses **rank()** with **ties.method=”first”** to assign a rank to each player based on the value in the **points** column:

#create new column that ranks players based on their points value df$points_rank = rank(df$points, ties.method="first") #view updated data frame df player points points_rank 1 A 5 1 2 B 8 2 3 C 10 3 4 D 10 4 5 E 17 5

Since players C and D had the same number of points and were in rank positions 3 and 4, the first player between these two (player “C”) received the lower rank.

**Example 3: Use rank() with ties.method=”last”**

The following code shows how to create a new column that uses **rank()** with **ties.method=”last”** to assign a rank to each player based on the value in the **points** column:

#create new column that ranks players based on their points value df$points_rank = rank(df$points, ties.method="last") #view updated data frame df player points points_rank 1 A 5 1 2 B 8 2 3 C 10 4 4 D 10 3 5 E 17 5

Since players C and D had the same number of points and were in rank positions 3 and 4, the last player between these two (player “D”) received the lower rank.

**Example 4: Use rank() with ties.method=”min”**

The following code shows how to create a new column that uses **rank()** with **ties.method=”min”** to assign a rank to each player based on the value in the **points** column:

#create new column that ranks players based on their points value df$points_rank = rank(df$points, ties.method="min") #view updated data frame df player points points_rank 1 A 5 1 2 B 8 2 3 C 10 3 4 D 10 3 5 E 17 5

Since players C and D had the same number of points and were in rank positions 3 and 4, both players received the minimum rank: **3**.

**Example 5: Use rank() with ties.method=”max”**

The following code shows how to create a new column that uses **rank()** with **ties.method=”max”** to assign a rank to each player based on the value in the **points** column:

#create new column that ranks players based on their points value df$points_rank = rank(df$points, ties.method="max") #view updated data frame df player points points_rank 1 A 5 1 2 B 8 2 3 C 10 4 4 D 10 4 5 E 17 5

Since players C and D had the same number of points and were in rank positions 3 and 4, both players received the maximum rank: **4**.

**Example 6: Use rank() with ties.method=”random”**

The following code shows how to create a new column that uses **rank()** with **ties.method=”random”** to assign a rank to each player based on the value in the **points** column:

#create new column that ranks players based on their points value df$points_rank = rank(df$points, ties.method="random") #view updated data frame df player points points_rank 1 A 5 1 2 B 8 2 3 C 10 4 4 D 10 3 5 E 17 5

Since players C and D had the same number of points and were in rank positions 3 and 4, each player was randomly assigned a rank of either 3 or 4.

Note that when you use “random” for the **ties.method**, the rank assigned to each value can change each time you run the code.

**Additional Resources**

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

How to Use sort(), order(), and rank() in R

How to Calculate Percentile Rank in R

How to Rank by Group Using dplyr