R Tutorial R Charts & Graphs R Statistics R References

R - quantile() Function



The R quantile() function returns sample quantiles of the argument corresponding to the specified probabilities. The syntax for using this function is given below:

Syntax

quantile(x, probs, na.rm = FALSE)

Parameters

x Required. Specify numeric vector whose sample quantiles are calculated.
prob Optional. Specify numeric vector of probabilities with values in range [0, 1]. Default is c(0, 0.25, 0.5, 0.75, 1).
na.rm Optional. Specify TRUE to remove NA or NaN values before the computation. Default is FALSE.

Return Value

Returns sample quantiles of the argument corresponding to the specified probabilities.

Example:

The example below shows the usage of quantile() function.

vec <- c(10, 55, 25, -56, 30, 99, 102, 67.34)

#calculating quantiles without prob parameter
cat("quantiles of vec:", quantile(vec), "\n")

#defining probabilities for quantiles
p <- c(0.25, 0.5, 0.75, 0.99)
#calculating quantiles using prob parameter
cat("quantiles of vec:", quantile(vec, prob=p), "\n")

The output of the above code will be:

quantiles of vec: -56 21.25 42.5 75.255 102 
quantiles of vec: 21.25 42.5 75.255 101.79 

Using na.rm parameter

The na.rm parameter can be set TRUE to remove NA or NaN values before the computation.

Example:

Consider the example below to see the usage of na.rm parameter.

vec <- c(10, 55, 25, -56, 30, 99, 102, 67.34, NA, NaN)

p <- c(0.25, 0.5, 0.75, 0.99)
#calculating quantiles after removing NA and NaN
cat("quantiles of vec:", quantile(vec, prob=p, na.rm=TRUE))

The output of the above code will be:

quantiles of vec: 21.25 42.5 75.255 101.79

❮ R Statistical Functions