Open In App

Create a ranking variable with Dplyr package in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to create a ranking variable with the Dplyr package in R.

Installation

To install this package type the below command in the terminal.

install.packages("dplyr") 

The mutate method can be used to rearrange data into a different orientation by performing various aggregate and statistic method and assigning it to new column names of the data frame. 

Syntax:

mutate(new-col-name = function(col-name))

The desc() method can be used to arrange the data in descending order. It is the in-built aggregate method available in R. However, along with a – sign in front of the column, is used to assign the ranking variable in ascending order. 

Syntax:

desc(col-name)

The dense_rank method can be applied in order to return the rank of rows within a window partition, without any gaps. It takes as an argument the column name of the data frame.  The variable with the same value is assigned the same rank irrespective of the number of times it appears within the column of the data frame. 

Example 1:

R




library(data.table)
library(dplyr)
  
# creating first data frame
data_frame < - data.table(col1=rep(c(5: 7), each=2),
                          col2=c(1, 4, 3, 4, 1, 6),
                          col3=1
                          )
print("Original DataFrame")
print(data_frame)
  
# ranking variable in data frame
print("Modified DataFrame")
  
# ranking by column 2
data_frame % > % mutate(rank=dense_rank(desc(-col2)))


Output

[1] "Original DataFrame" 
 col1 col2 col3 
1:    5    1    1 
2:    5    4    1 
3:    6    3    1 
4:    6    4    1 
5:    7    1    1 
6:    7    6    1 
[1] "Modified DataFrame" 
col1 col2 col3 rank 
1:    5    1    1    1 
2:    5    4    1    3 
3:    6    3    1    2 
4:    6    4    1    3 
5:    7    1    1    1 
6:    7    6    1    4

Example 2:  Assigns the ranks in descending order

The column name variable rank can also be arranged in the data frame without a minus sign, which assigns the ranks in descending order. 

R




library(data.table)
library(dplyr)
  
# creating first data frame
data_frame < - data.table(col1=rep(c(5: 7), each=2),
                          col2=c(1, 4, 3, 4, 1, 6),
                          col3=1
                          )
  
print("Original DataFrame")
print(data_frame)
  
# ranking variable in data frame
print("Modified DataFrame")
data_frame % > % mutate(rank=dense_rank(desc(col2)))


Output

[1] "Original DataFrame"
col1 col2 col3 
1:    5    1    1 
2:    5    4    1 
3:    6    3    1 
4:    6    4    1 
5:    7    1    1 
6:    7    6    1 
[1] "Modified DataFrame" 
col1 col2 col3 rank 
1:    5    1    1    4 
2:    5    4    1    2 
3:    6    3    1    3 
4:    6    4    1    2 
5:    7    1    1    4 
6:    7    6    1    1


Last Updated : 24 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads