How to find rank and dense rank in R

This recipe helps you find rank and dense rank in R

Recipe Objective

How to find rank and dense rank in R ?

A rank () function in R determines a distinct rank for all the given input data points. The rank () helps in understanding where the low and high points are in the range of data points. The dense_rank () function in R also assigns a distinct rank for all the given input data points. The difference between rank () and dense_rank () is, when the elements in a vector are repeated say twice, the rank () assigns the same rank to both the elements skipping the next rankings. While a dense_rank (), assigns the same ranking to the elements without skipping the next rankings. This recipe demonstrates an example of rank () and dense_rank () in R.

Step 1 - Install the required library

install.packages("dplyr") library(dplyr)

Step 2 - Define a vector

x <- c(1,6,13,4,12,3,8,14,23,7)

Step 3 - Use rank() and dense_rank()

rank(x) # returns ranking for every element of the defined vector x

 "Output of code :"
1 4 8 3 7 2 6 9 10 5

dense_rank(x) # returns ranking for every element of the defined vector x

 "Output of code :"
1 4 8 3 7 2 6 9 10 5

Step 4 - rank() and dense_rank() for repeated values

x <- c(1,1,13,4,12,4,3,14,23,7) rank(x)

 "Output of code :"
1.5 1.5 8 4.5 7 4.5 3 9 10 6

In the rank(), 1.5 is the rank assigned to repeated values (1,1) and it skips the rank - 1 as well as the rank - 2 and directly jumps on to rank - 3. Also the next repeated elements in the vector (4,4) get a rank assigned as 4.5, skipping the next rank - 5 and directly jumps on the rank - 6.

dense_rank(x)

 "Output of code :"
1 1 6 3 5 3 2 7 8 4

In the dense_rank() however, (1,1) rank is assigned to both the repeated values 1 and without skipping or leaving any gaps, i.e rank-2 is assigned to the next elements in the vector. Similarly, for the repeated elements(4,4), ranks are assigned as rank - 3 and the next ranking is assigned as rank - 4, i.e no gaps are present in the ranking.

{"mode":"full","isActive":false}

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.