How to add a rank column in base R of a data frame?

R ProgrammingServer Side ProgrammingProgramming

Ranking of a variable has many objectives such as defining order based on hierarchy but in data science, we use it mainly for analyzing non-parametric data. The ranking of a variable in an R data frame can be done by using rank function. For example, if we have a data frame df that contains column x then rank of values in x can be found as rank(df$x).

Example

 Live Demo

Consider the below data frame:
x1<-sample(c("A","B","C"),20,replace=TRUE)
y1<-rpois(20,2)
df1<-data.frame(x1,y1)
df1

Output

  x1 y1
1  A 4
2  A 0
3  A 2
4  B 1
5  A 0
6  C 6
7  A 4
8  C 0
9  C 4
10 B 1
11 A 2
12 B 2
13 B 2
14 B 3
15 C 0
16 B 1
17 C 3
18 C 4
19 C 2
20 C 2

Creating a column with Ranks of values in y1 −

Example

df1$Rank<-rank(df1$y1)
df1

Output

   x1 y1 Rank
1  A 4 17.5
2  A 0 2.5
3  A 2 10.5
4  B 1 6.0
5  A 0 2.5
6  C 6 20.0
7  A 4 17.5
8  C 0 2.5
9  C 4 17.5
10 B 1 6.0
11 A 2 10.5
12 B 2 10.5
13 B 2 10.5
14 B 3 14.5
15 C 0 2.5
16 B 1 6.0
17 C 3 14.5
18 C 4 17.5
19 C 2 10.5
20 C 2 10.5

Example

 Live Demo

x2<-sample(c("F1","F2","F3","F4"),20,replace=TRUE)
y2<-sample(0:9,20,replace=TRUE)
df2<-data.frame(x2,y2)
df2

Output

  x2 y2
1  F1 3
2  F1 4
3  F3 3
4  F4 1
5  F4 2
6  F2 9
7  F4 5
8  F1 1
9  F2 8
10 F2 1
11 F2 3
12 F4 0
13 F1 7
14 F4 5
15 F3 3
16 F2 9
17 F4 6
18 F4 6
19 F4 9
20 F2 9

Creating a column with Ranks of values in y2 −

Example

df2$Rank<-rank(df2$y2)
df2

Output

   x2 y2 Rank
1  F1 3 7.5
2  F1 4 10.0
3  F3 3 7.5
4  F4 1 3.0
5  F4 2 5.0
6  F2 9 18.5
7  F4 5 11.5
8  F1 1 3.0
9  F2 8 16.0
10 F2 1 3.0
11 F2 3 7.5
12 F4 0 1.0
13 F1 7 15.0
14 F4 5 11.5
15 F3 3 7.5
16 F2 9 18.5
17 F4 6 13.5
18 F4 6 13.5
19 F4 9 18.5
20 F2 9 18.5
raja
Published on 05-Dec-2020 13:13:57
Advertisements