

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 create a rank variable using mutate function of dplyr package in R?
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
- Related Questions & Answers
- How to rename the factor levels of a factor variable by using mutate of dplyr package in R?
- How to create a new column for factor variable with changed factor levels by using mutate of dplyr package in R?
- How to extract columns of a data frame in R using dplyr package?
- How to convert numeric columns to factor using dplyr package in R?
- How to remove multiple rows from an R data frame using dplyr package?
- How to create relative frequency table using dplyr in R?
- How to find the mean of each variable using dplyr by factor variable with ignoring the NA values in R?
- How to add a new column to a data frame using mutate in R?
- How to create a dummy variable in R?
- How to find the frequency of a particular string in a column based on another column in an R data frame using dplyr package?
- How to create a categorical variable using a data frame column in R?
- How to find the rank of a matrix in R?
- How to select rows with group wise minimum or maximum values of a variable in an R data frame using dplyr?
- How to detach a package in R?
- How to create a scatterplot with log10 of dependent variable in R?
Advertisements