There is no inverse function of quantile
but it is not difficult to get the percentile of any value x0
.
x0 <- 35
First it is determined with findInterval
where is x0
in the vector x
ordered. To have quantis, divide by the length of the vector.
onde1 <- findInterval(x0, sort(x))/length(x)
quantile(x, onde1)
# 60%
#35.4
x0
must correspond to a quantity below 60%, therefore the range on the left is opened.
onde2 <- findInterval(x0, sort(x), left.open = TRUE)/length(x)
quantile(x, onde2)
# 52%
#33.96
Closing the gap on the right wouldn’t do any good:
onde3 <- findInterval(x0, sort(x), rightmost.closed = TRUE)/length(x)
quantile(x, onde3)
# 60%
#35.4
Now calculate the requested value with approx
.
qq <- quantile(x, probs = c(onde3, onde1))
xqq <- unname(qq)
yqq <- as.numeric(sub("%", "", names(qq)))
approx(xqq, yqq, xout = x0)
#$x
#[1] 35
#
#$y
#[1] 57.77778