Rank Function in R

rank() function in R returns the ranks of the values in a vector. rank function in R also handles Ties and missing values in several ways. Rank of the vector with NA. Min rank, Max rank, last rank and average rank in R. rank() function in R returns the rank of the column in R. We can also calculate minimum and maximum rank of the column in R dataframe.

  • Rank the Vector in R by descending order, by minimum rank, maximum rank, first rank, last rank and average of two ranks if two values are found same
  • Rank the dataframe in R by ascending and descending order
  • Rank the dataframe column by minimum rank if found 2 values are same
  • Rank the dataframe by Maximum rank if found 2 values are same
  • Rank the dataframe column by first,  last and average of two rank if found 2 values are same
  • Rank the dataframe of the character column in R using rank() function.

 

Syntax for rank function in R:

rank(x, na.last = TRUE, ties.method = c(“average”, “first”, “random”, “max”, “min”))
x  numeric, character or logical vector
na.last Treatment of NAs. How to Handle NAs
ties.method Treatment of Ties. How to Handle Ties

Rank function in R with NAs as last:

x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x)

by default NAs are ranked last, so the output will be

[1]  3  4  2  1  8  7  6  5

 

Rank function in R with NAs as First:

NAs are ranked first

x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = FALSE) #NA FIRST
output:
[1]  4  5  3  2  1  8  7  6

 

Rank function in R with NAs are removed:

NAs are neglectled by rank function

x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = NA) #NA removed
output:
[1]  3  4  2  1  7  6  5

 

Rank a vector in R with NAs are ranked as NA:

NAs are kept and ranked as NAs by rank function

x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = "keep") #NA is kept with rank NA
output:
[1]  3  4  2  1 NA  7  6  5

 

Rank function in R with Ties=”Average”:

Average of the rank is allocated to the elements of vector when ties are encountered

x <- c(2,7,1,-17,NA,Inf,35,21,7)
rank(x,na.last = TRUE,ties.method = "first")
output:
[1] 3.0   4.5   2.0   1.0   9.0   8.0   7.0   6.0   4.5

 

Rank function in R with Ties=”random”:

x <- c(2,7,1,-17,NA,Inf,35,21,7)
rank(x,na.last = TRUE,ties.method = "random")
output:
[1] 3  4  2  1  9  8  7  6  5

 

Rank function in R with Ties=”max”:

Maximum value of the rank is allocated to the elements of vector when ties are encountered

x <- c(2,7,1,-17,NA,Inf,35,21,7)
rank(x,na.last = TRUE,ties.method = "max")
output:
[1]  3  5  2  1  9  8  7  6  5

 

Rank function in R with Ties=”min”:

Minimum value of the rank is allocated to the elements of vector when ties are encountered

x <- c(2,7,1,-17,NA,Inf,35,21,7)
rank(x,na.last = TRUE,ties.method = "min")
output:
[1]  3  4  2  1  9  8  7  6  4

 

Ranking of character vector in R:

Rank() function can also be used to rank character vector

y<-c("a","h","e","d")
rank(y)
output:
[1]  1  4  3  2

 

 

 

Rank the dataframe columns in R

First lets create the dataframe as shown below

#### Create dataframe
my_basket = data.frame(ITEM_GROUP = c("Fruit","Fruit","Fruit","Fruit","Fruit","Vegetable","Vegetable","Vegetable","Vegetable","Dairy","Dairy","Dairy","Dairy","Dairy"), 
                       ITEM_NAME = c("Apple","Banana","Orange","Mango","Papaya","Carrot","Potato","Brinjal","Raddish","Milk","Curd","Cheese","Milk","Paneer"),
                       Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120))

my_basket

so the resultant dataframe will be

Rank function in R 11

 

Rank the dataframe column in ascending order R

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, average rank of both the values are assigned as shown below.


###### Rank the dataframe column in R

my_basket$Price_rank = rank(my_basket$Price)
my_basket

so the resultant dataframe with rank calculated will be

Rank function in R 12

 

 

Rank the dataframe column in descending order R

Rank of “Price” column in desending order is calculated and assigned to the column “Price_rank”. in case of same values, average rank of both the values are assigned as shown below.


###### Rank the dataframe column in R

my_basket$Price_rank = rank(desc(my_basket$Price))
my_basket

so the resultant dataframe with rank calculated will be

Rank function in R 13

 

 

Rank the dataframe column in ascending order with random ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, random rank of both the values are assigned as shown below.


###### Rank the dataframe column in R

my_basket$Price_rank = rank(my_basket$Price,na.last = TRUE,ties.method = "random")
my_basket

so the resultant dataframe with rank calculated will be
Rank function in R 14

 

 

Rank the dataframe column in ascending order with max ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, maximum rank of both the values in case of ties  are assigned as shown below.


###### Rank the dataframe column in R

my_basket$Price_rank = rank(my_basket$Price,na.last = TRUE,ties.method = "max")
my_basket

so the resultant dataframe with rank calculated will be

Rank function in R 15

 

 

Rank the dataframe column in ascending order with min ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, minimum rank of both the values in case of ties  are assigned as shown below.


###### Rank the dataframe column in R

my_basket$Price_rank = rank(my_basket$Price,na.last = TRUE,ties.method = "min")
my_basket

so the resultant dataframe with rank calculated will be

Rank function in R 16

 

Rank the dataframe column in ascending order with First ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, First rank is assigned to the first value in case of ties  are assigned as shown below.


###### Rank the dataframe column in R

my_basket$Price_rank = rank(my_basket$Price,na.last = TRUE,ties.method = "first")
my_basket

so the resultant dataframe with rank calculated will be

Rank function in R 17

 

Rank the dataframe column in ascending order with last ranking method

Rank of “Price” column in ascending order is calculated and assigned to the column “Price_rank”. in case of same values, Last rank is assigned to the first value in case of ties  are assigned as shown below.


###### Rank the dataframe column in R

my_basket$Price_rank = rank(my_basket$Price,na.last = TRUE,ties.method = "last")
my_basket

so the resultant dataframe with rank calculated will be

Rank function in R 18

 

Rank the character column of the dataframe  in R

Rank of “ITEM_NAME” column in ascending order is calculated and assigned to the column “ITEM_NAME_ALphabet_RANK”.


###### Rank the dataframe column in R

my_basket$ITEM_NAME_ALphabet_RANK = rank(my_basket$ITEM_NAME)
my_basket

so the resultant dataframe with rank calculated will be

Rank function in R 20

 

previous small rank function in r                                                                                                           next small rank function in r

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.