Open In App

How to Calculate Quantiles by Group in R?

Last Updated : 23 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

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

Syntax:

quantile( data, probs)

Parameters:

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

To group data, we use dplyr module. This module contains a function called group_by() in which the column to be grouped by has to be passed.

Syntax:

group_by(column_name)

To find quantiles of the grouped data we will call summarize method with quantiles() function.

Syntax:

summarize( function )

Example 1: Calculate Quantiles by group by summarizing one quartile with probability 0.5

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'))
 
# create groups
# calculate quantiles by group
df %>% group_by(y) %>%
  summarize(res=quantile(x,probs=0.5))


Output:

Example 2: Calculate quantiles by group by summarizing three quartiles with probability 0.25, 0.5, and 0.75.

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'))
 
# create groups
# find quantiles
df %>% group_by(y) %>%
  summarize(first=quantile(x,probs=0.25),
            second=quantile(x,probs=0.5),
            third=quantile(x,probs=0.75))


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads