- 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 convert data frame values to their rank in R?
To convert data frame values to their rank in R, we can follow the below steps −
- First of all, create a data frame.
- Then, find the rank of each value using rank function with lapply.
Create the data frame
Let's create a data frame as shown below −
> x1<-rnorm(20) > y1<-rnorm(20) > df<-data.frame(x1,y1) > df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x1 y1 1 -0.9045608 1.95315551 2 -1.6222122 -1.19880638 3 0.8618855 -0.33643175 4 -0.3547085 -0.18097356 5 -0.3043621 -0.60342210 6 0.5606001 -0.83618995 7 -1.1752752 -0.46651702 8 0.2009474 -0.39482238 9 0.3595199 -0.59964119 10 -0.6007706 -0.01499288 11 0.1972236 1.44063062 12 1.0988526 0.17336280 13 1.0016738 2.27343055 14 2.6411272 -0.21273651 15 -1.1907730 0.88769339 16 1.0993620 1.57416767 17 -0.6732082 2.44837538 18 2.7868490 -0.46566411 19 0.9185654 -1.51994397 20 -1.2386102 1.34563437
Convert data frame values into their ranks
Using rank function with lapply function to find the rank of each value in the data frame df −
> df[]<-lapply(-df,rank,ties.method="min") > df
Output
x1 y1 1 16 3 2 20 19 3 7 12 4 13 10 5 12 17 6 8 18 7 17 15 8 10 13 9 9 16 10 14 9 11 11 5 12 4 8 13 5 2 14 2 11 15 18 7 16 3 4 17 15 1 18 1 14 19 6 20 20 19 6
- Related Articles
- How to convert negative values in an R data frame to positive values?
- How to convert empty values to NA in an R data frame?
- How to convert NaN values to NA in an R data frame?
- How to convert data frame values to a vector by rows in R?
- How to convert values in alternate rows to negative in R data frame column?
- How to find percentile rank for groups in an R data frame?
- How to convert an old data frame to new data frame in R?
- How to convert a character data frame to numeric data frame in R?
- How to add a rank column in base R of a data frame?
- How to convert the character values in an R data frame column to lower case?
- How to convert a data frame column to date that contains integer values in R?
- How to convert a data frame to data.table in R?
- How to convert alphabets to numbers in R data frame?
- How to convert a data frame into two column data frame with values and column name as variable in R?
- How to convert values greater than a threshold into 1 in R data frame column?

Advertisements