4

So for example, I have a discrete function with a CDF as follows:

cdf <- c(0.00, 0.35, 0.71, 0.92, 1.00, 1.00, 1.00, 1.00)

I can create a sort of quantile function with the following line . . .

result <- which(cdf == min(cdf[cdf > x]))

. . . where x is the cumulative probability. So for example, qfunction(0.9) = 4 and qfunction(0.99) = 5.

This solution appears fine (albeit inelegant) until I want to handle vectors. So if x = c(0.9, 0.99) my function falls over. This seems like something that people would do a lot of in R and yet I haven't found a solution. R is not my primary language.

Any help would be appreciated.

timbo
  • 1,533
  • 1
  • 15
  • 26

1 Answers1

7

You probably want the findInterval() function. See the ?findInerval help page for details about the function. But something like

findInterval(c(.9, .99), cdf)+1

should work for your sample data/input.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks. I thought it had to be this embarrassingly simple, but I just couldn't find it. – timbo Aug 03 '15 at 02:32