# quantile() function in R: Calculate the sample quantiles of the numeric dataset

In this article, we will show you what the quantile() function in R is and how to use the quantile() function in R. The quantile() function can help you calculate the sample quantiles of the numeric dataset. Let’s follow this article to learn how to use the quantile() function in R with the explanation and examples below.

## What does the quantile() function do in R?

The quantile() function is used to calculate the sample quantiles of the numeric dataset following the given probabilities. The smallest probability of observing is 0, and the largest probability is 1. Let’s take a look at the syntax of the quantile() function in the next title below.

Syntax:

quantile(x , probs, na.rm = FALSE, names = TRUE, type, …)

Parameters:

• x: The numeric vector. The NA values are not allowed if you set na.rm = FALSE.
• probs: The probability which value is in [0, 1].
• na.rm: The default is FALSE. Remove the NA values before performing the function or not.
• names: The default is TRUE. The result has a name attribute or not.
• type: The default is 7. The quantile algorithm to be used.

After learning what the quantile() function does in R and the syntax of it, you will learn how to use it in the next title below.

## How to use the quantile() function in R?

The quantile() function is used to calculate the sample quantiles of the numeric dataset.

### Use the quantile() function with the vector

You can use the quantile() function to calculate the sample quantiles of the numeric vector.

Look at the example below.

# Create the vector.
vec <- c (1,2,3,4,5,6,7,8,9,10,11,12)

# Calculate the sample quantiles.
quantile(vec, probs = seq(0,1,0.2))

Output

  0%  20%  40%  60%  80% 100%
1.0  3.2  5.4  7.6  9.8 12.0


### Calculate the sample quantile with the NA value

You can calculate the sample quantile with the object that has the NA values by the quantile() function. Simply, you only set na.rm = TRUE to do that.

Look at the example below.

# Create the vector.
vec <- c (1,2,3,4,5,6,7,8,9,10,NA,NA,NA)

# Calculate the sample quantiles.
quantile(vec, probs = seq(0,1,0.2), na.rm = TRUE)

Output

  0%  20%  40%  60%  80% 100%
1.0  2.8  4.6  6.4  8.2 10.0


### Use the quantile() function with the data frame

You can use the quantile() function to calculate the sample quantile of the column in the data frame.

Look at the example below.

# Create the data frame.
df <- data.frame( club = c('RMA','FCB','MU','PSG','MC'),
fans = c (1000, 900, 900, 300, 400))

# Calculate the sample quantile of fans.
quantile(df\$fans, probs = seq (0,1,0.2))

Output

  0%  20%  40%  60%  80% 100%
300  380  700  900  920 1000


## Summary

You have learned about what the quantile() function in R does and how to use it. By the quantile() function, you can calculate the sample quantiles of the numeric dataset. You can use it for the numeric vector or the numeric column in the data frame, … We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Posted in R