Open In App

How to Calculate Deciles in R?

Last Updated : 23 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to calculate deciles in the R programming language. 

Deciles are numbers that split a dataset into ten groups, each of equal frequency.

Method 1: Using quantiles to calculate Deciles

In this method, we use quantile() method. For this probs, parameters have to be varied. 

Syntax:

quantile(data, probs)

Parameter:

  • data is the data in consideration
  • probs is the required percentile

Example: 

In this example, we calculate deciles using quantile() function in the R language. 

R




# create dataframe
df<-data.frame(x=c(2,13,5,36,12,50),
               y=c('a','b','c','c','c','b'))
 
# calculate deciles
res<-quantile(df$x, probs=seq(0.1,1, by=0.1))
 
# display
res


 
 

Output:

 

 10%  20%  30%  40%  50%  60%  70%  80%  90% 100%  

3.5  5.0  8.5 12.0 12.5 13.0 24.5 36.0 43.0 50.0 

Method 2: Using ntile to calculate Deciles

ntile() function belongs to dplyr package. This function will automatically divide the frequencies and place them in required group 

Syntax:

ntile(data, num)

Parameter:

  • data in consideration
  • num is the required number of groups

Example: 

In this example, we calculate deciles using ntile() function in the R language.

R




# import library
library(dplyr)
 
# create dataframe
df<-data.frame(x=c(2,13,5,36,12,50),
               y=c('a','b','c','c','c','b'))
 
# calculate deciles
res<-ntile(df,10)
 
# display
res


 
Output:

 [1]  1  2  1  3  2  4  5  6  8  9 10  7



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads