Open In App

How to Calculate Quartiles in R?

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

Quartiles are just special percentiles that occur after a certain percent of data has been covered.



To obtain the required quartiles, the quantile() function is used.

Syntax: quantile( data, probs)



Parameter:

  • data: data whose percentiles are to be calculated
  • probs: percentile value

Example 1: Calculate quartile in vector




x = c(2,13,5,36,12,50)
  
res<-quantile(x, probs = c(0,0.25,0.5,0.75,1))
res

Output:

   0%   25%   50%   75%  100%  
2.00  6.75 12.50 30.25 50.00 

Example 2: Calculate quartile in dataframe




df<-data.frame(x = c(2,13,5,36,12,50),
y = c('a','b','c','c','c','b'))
  
res<-quantile(df$x, probs = c(0,0.25,0.5,0.75,1))
res

Output:

  0%   25%   50%   75%  100%  
2.00  6.75 12.50 30.25 50.00 
Article Tags :