How to create a rank variable using mutate function of dplyr package in R?

R ProgrammingServer Side ProgrammingProgramming

A rank variable is created to convert a numerical variable into ordinal variable. This is useful for non-parametric analysis because if the distribution of the numerical variable is not normal or there are assumptions of parametric analysis that cannot be followed by the numerical variable then the raw variable values are not analyzed directly. To create a rank variable using mutate function, we can use dense_rank argument.

Example

Consider the below data frame −

set.seed(7)
x1 <-sample(1:5,20,replace=TRUE)
x2 <-rep(c("Winter","Spring","Summer","Rainy"),times=5)
df <-data.frame(x1,x2)
df

Output

x1 x2
1 2 Winter
2 3 Spring
3 4 Summer
4 2 Rainy
5 2 Winter
6 3 Spring
7 3 Summer
8 2 Rainy
9 4 Winter
10 3 Spring
11 4 Summer
12 2 Rainy
13 3 Winter
14 5 Spring
15 4 Summer
16 3 Rainy
17 2 Winter
18 2 Spring
19 4 Summer
20 3 Rainy
library(dplyr)

Creating a rank variable for x1 variable −

Example

df%>%mutate(Rank_x1=dense_rank(desc(-x1)))

Output

x1 x2 Rank_x1
1 2 Winter 1
2 3 Spring 2
3 4 Summer 3
4 2 Rainy 1
5 2 Winter 1
6 3 Spring 2
7 3 Summer 2
8 2 Rainy 1
9 4 Winter 3
10 3 Spring 2
11 4 Summer 3
12 2 Rainy 1
13 3 Winter 2
14 5 Spring 4
15 4 Summer 3
16 3 Rainy 2
17 2 Winter 1
18 2 Spring 1
19 4 Summer 3
20 3 Rainy 2

Let’s have a look at another example −

Example

grp <-rep(c(28,29,31,45,37),times=4)
Percentage <-rep(c(28,29,31,45,37),times=4)
ID <-1:20
df_new <-data.frame(ID,Percentage)
df_new

Output

ID Percentage
1 1 28
2 2 29
3 3 31
4 4 45
5 5 37
6 6 28
7 7 29
8 8 31
9 9 45
10 10 37
11 11 28
12 12 29
13 13 31
14 14 45
15 15 37
16 16 28
17 17 29
18 18 31
19 19 45
20 20 37

Example

df%>%mutate(Rank_Percentage=dense_rank(desc(-Percentage)))

Output

x1 x2 Rank_Percentage
1 2 Winter 1
2 3 Spring 2
3 4 Summer 3
4 2 Rainy 5
5 2 Winter 4
6 3 Spring 1
7 3 Summer 2
8 2 Rainy 3
9 4 Winter 5
10 3 Spring 4
11 4 Summer 1
12 2 Rainy 2
13 3 Winter 3
14 5 Spring 5
15 4 Summer 4
16 3 Rainy 1
17 2 Winter 2
18 2 Spring 3
19 4 Summer 5
20 3 Rainy 4
raja
Published on 24-Aug-2020 13:05:10
Advertisements