0

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)

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Rik
  • 1,870
  • 3
  • 22
  • 35

2 Answers2

3

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
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Good answer thanks, just chose the one that came in first since they are similar. – Rik May 07 '15 at 02:35
2

Try findInterval, e.g.:

findInterval(c(0.1, 0.98), cdf) + 1
# [1] 1 7
sgibb
  • 25,396
  • 3
  • 68
  • 74