Home > other >  Find what percentile a number ranks within a given range?
Find what percentile a number ranks within a given range?

Time:10-30

suppose I have a number distribution like this.

set.seed( 145)
data <- runif(100, 0, 500)
q = quantile( data, probs=c ( seq(0,1, by=.01) ) )

for a given number, 300, I would like to know what percentile it ranks in data? I can I guess figure out which q bin it is between but this is convoluted, is there an easier way?

thanks.

CodePudding user response:

We may use findInterval

names(q)[findInterval(300, q)  1]
[1] "54%"

where q corresponds to

> q[55]
     54% 
306.9737 
  • Related