Open In App

How to Calculate Deciles in R?

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:

Example: 

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




# 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:

Example: 

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




# 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


Article Tags :