MMEL
MMEL

Reputation: 358

R calculates quantile wrong or different?

I found an article that provides the algorithm for calculating quantile and R is apparently not following that article.

The article is: https://www-users.york.ac.uk/~mb55/intro/quantile.htm

in R, I have the following code:

nv<-c(10,20,30,40)
quantile(nv)
  0%  25%  50%  75% 100% 
10.0 17.5 25.0 32.5 40.0

However, it seems that the quantile for 75% result is wrong as according to the article, the formula is:

i = q(n+1) 

and in my case(75%) q=0.75 and n=4 (4 observations in my combination)

X_j + (X_j+1 - X_j) times (i - j) 

That means that it should be:

30 + (40-30)*(3.75-3) = 37.5 and not 32.5

I am having a hard time thinking that R made a mistake.

What am I missing here?

Thank you.

Upvotes: 3

Views: 2468

Answers (1)

neilfws
neilfws

Reputation: 33782

If you look at the help page for quantile:

?quantile

you will see that quantiles can be calculated in different ways, which can be specified using the type = argument, with an integer from 1-9.

Type 6 gives the result that you were expecting:

quantile(c(10, 20, 30, 40), type = 6)

  0%  25%  50%  75% 100% 
10.0 12.5 25.0 37.5 40.0

Upvotes: 7

Related Questions