user3022875 user3022875 - 1 year ago 57
R Question

find the quantile percentage given a value

I have a data in the t vector and given the data I can get quantile

t = c(1,2,3,4,5,6,3,2,21,22,23,2,23,4,4,4444)

But I would like to do the opposite

I want provide 3 and have it return the quantile percentage

and the output is 30%

Is there a function for that?

Answer Source

If x is the vector, and x0 is the value you want to check, we can use

match(x0, sort(x)) / (length(x) + 1)

For your example:

x <- c(1,2,3,4,5,6,3,2,21,22,23,2,23,4,4,4444)
match(3, sort(x)) / (length(x) + 1)
# [1] 0.2941176