26
$\begingroup$

I have a set of real numbers. I need to estimate the quantile of a new number. Is there any clean way to do this in R? in general?

I hope this is not ultra-trivial ;-)

Much appreciated for your response.

PK

$\endgroup$
35
$\begingroup$

As whuber pointed out, you can use ecdf, which takes a vector and returns a function for getting the percentile of a value.

> percentile <- ecdf(1:10)
> percentile(8)
[1] 0.8
$\endgroup$
10
$\begingroup$

To expand on what whuber and cwarden stated, sometimes you want to use a function in a "classical" R way. Then you could write it youself using ecdf():

ecdf_fun <- function(x,perc) ecdf(x)(perc)
ecdf_fun(1:10,8)
>[1] 0.8
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.