Skip to content
Prev 256937 / 398506 Next

Find number of elements less than some number: Elegant/fastsolution needed

Ravi:

Well, that's a challenge!

Here is a solution which is faster than Bill's as long as length(y) << length(x).

f3 <- function(x, y) length(y)*ecdf(y)(x)

Then using your test on my workstation, I get for f2():
[1]  0.761  1.071  1.329  1.565  1.894  3.865 11.824

and for f3():
[1]  0.717  0.882  1.010  1.161  1.389  3.080 15.530

I suspect ecdf() is a little more intuitive than findInterval().

Hope this helps,
Ray Brownrigg
On Fri, 15 Apr 2011, Ravi Varadhan wrote: