Open In App

How to Calculate Quartiles in R?

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  • First quartile: Refers to 25th percentile of the data. This depicts that 25% percent of data is under the produced value.
  • Second quartile: Refers to 50th percentile of the data. This depicts that 50% percent of data is under the produced value. This is also the median of the data.
  • Third quartile: Refers to the 75th percentile of the data. This predicts that 75% percent of the data is under the produced value.

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

R
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

R
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

Next Article
Article Tags :

Similar Reads

  翻译: