- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add a rank column in base R of a data frame?
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
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
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
- Related Articles
- How to add a new column to a data frame using mutate in R?
- How to add a column in an R data frame with consecutive numbers?
- How to match a column in a data frame with a column in another data frame in R?
- How to add a column between columns or after last column in an R data frame?
- How to extract a single column of an R data frame as a data frame?
- How to add columns with square of each column in R data frame?
- How to convert data frame values to their rank in R?
- How to add a new column at the front of an existing R data frame?
- How to add a new column in an R data frame with count based on factor column?
- How to add a string before each numeric value in an R data frame column?
- How to add a row to a frame from another data frame in R?
- How to assign a column value in a data frame based on another column in another R data frame?
- How to add a data frame inside a list in R?
- How to standardized a column in an R data frame?
- How to split a data frame by column in R?

Advertisements