jf328 jf328 - 1 year ago 79
R Question

R fast method to find closest value in vector B for each element in A

I have a very large unsorted vector A and a sorted vector B (relatively short).

A = runif(n = 1e6)
B = seq(0,1,by = 1e-3)

Now given a direction 'forward' or 'backward', for each element in A, find the nearest element in B with that direction. Eg for 'forward'

A2 = sapply(A, function(x) B[B>=x][1])

gives the result. However, this is too slow as
loops over A.

> system.time(sapply(A, function(x) B[B>=x][1]))
user system elapsed
17.93 0.00 17.93

Is there a way to do this much faster?

(It is guaranteed that
, if this is useful)

Answer Source

The findInterval function solves this exact problem, using binary search. Try this:


A comparison:

A <- runif(n = 1e6)
B <- seq(0,1,by = 1e-3)
system.time(A2<-sapply(A, function(x) B[B>=x][1]))
#   user  system elapsed 
# 18.058   0.000  15.606
#   user  system elapsed 
#   0.00    0.00    0.07
#[1] TRUE
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download