41

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

kjetil b halvorsen
  • 63,378
  • 26
  • 142
  • 467
polarise
  • 543
  • 1
  • 4
  • 7

2 Answers2

52

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
cwarden
  • 678
  • 8
  • 7
16

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

ecdf_fun <- function(x,perc) ecdf(x)(perc)
ecdf_fun(1:10,8)
>[1] 0.8
zerweck
  • 291
  • 2
  • 6