All,
I have an array as follows:
x=runif(1)
cdf=cumsum(c(.2,.5,.1,.05,.05,.01,.09))
>p
[1] 0.20 0.70 0.80 0.85 0.90 0.91 1.00
How do I return the index of the corresponding cdf entry for x? for example, .1 would return 1, .98 would return 7)
As @sgibb demonstrates findInterval
is the right tool for that job. (I do not think that "empirical cdf" is the right term for what you want since the ecdf
will return a value in the range of the values. You are requesting the "order statistic". I added the closest available tag, but since SO is really not a statistics website the more narrow tag of "order statistic" is not found.) I have taken to surrounding my 'vec' arguments with -Inf
and Inf
( rather than incrementing the returned values by 1):
> findInterval(c(0.1, 0.98), c(-Inf,cdf,Inf))
[1] 1 7