haimen
haimen

Reputation: 2015

Finding percentile of a particular input in R

I have a dataset column which contains values. When a new input is given, I want to check this column and finding the percentile of that input value in that column.

I tried with quantile function. But the quantile function gives the values of 25th,50th percentile and so on. But I want the reverse of it. I want the percentile of a given value.

The following is my reproducible example,

data <- seq(90,100,length.out=1000)
input <- 97

My output should be the percentile of 97 in the data column. Is this possible to do? Thanks

Upvotes: 2

Views: 3743

Answers (2)

Julius Vainora
Julius Vainora

Reputation: 48241

You may also use a somewhat more statistical version with an empirical cumulative distribution function:

ecdf(data)(input)

or

F <- ecdf(data)
F(input)

This approach also allows for vectorization over input.

Upvotes: 5

Ben Bolker
Ben Bolker

Reputation: 226731

I think you want to count the fraction of the data that are (is?) less than the input value:

mean(input>data)
## [1] 0.7

Upvotes: 3

Related Questions