Quartile in R – Efficient Ways To Calculate

We’re going to show you how to calculate a quartile in R. This is particularly useful when you’re doing exploratory analysis and reporting, especially if you’re analyzing data which may not be normally distributed.

We’re going to use the r quantile function; this utility is part of base R (so you don’t need to import any libraries) and can be adapted to generate a variety of “rank based” statistics about your sample.

To calculate a quartile in R, set the percentile as parameter of the quantile function. You can use many of the other features of the quantile function which we described in our guide on how to calculate percentile in R.

In the example below, we’re going to use a single line of code to get the quartiles of a distribution using R.

# quartile in R example
> test = c(9,9,8,9,10,9,3,5,6,8,9,10,11,12,13,11,10)

# get quartile in r code (single line)
> quantile(test, prob=c(.25,.5,.75))
25% 50% 75% 
  8   9  10 

You can also use the summary function to generate the same information.

# quartile in R example - summary function
> test = c(9,9,8,9,10,9,3,5,6,8,9,10,11,12,13,11,10)
> summary(test)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  3.000   8.000   9.000   8.941  10.000  13.000 

Related Materials