Say we have a couple vectors
a <- c(1, 2, 2, 4, 7)
b <- c(1, 2, 3, 5, 7)
For each element b[i]
in b
I want find the number of elements in a
that's less than b[i]
, or, equivalent, I want to know the rank of b_i in c(b[i], a)
.
there are a couple naive ways I can think of, e.g. doing either of the following length(b)
times:
min_rank(c(b[i], a))
sum(a < b[i])
What's the best way to do this if length(a)
= length(b)
= N where N is large?
EDIT:
To clarify, I'm wondering if there's a more computationally efficient way to do this, i.e. if I can do better than quadratic time in this case.
Vectorization is always cool though ;), thanks @Henrik!
Running time
a <- rpois(100000, 20)
b <- rpois(100000, 10)
system.time(
result1 <- sapply(b, function(x) sum(a < x))
)
# user system elapsed
# 71.15 0.00 71.16
sw <- proc.time()
bu <- sort(unique(b))
ab <- sort(c(a, bu))
ind <- match(bu, ab)
nbelow <- ind - 1:length(bu)
result2 <- sapply(b, function(x) nbelow[match(x, bu)])
proc.time() - sw
# user system elapsed
# 0.46 0.00 0.48
sw <- proc.time()
a1 <- sort(a)
result3 <- findInterval(b - sqrt(.Machine$double.eps), a1)
proc.time() - sw
# user system elapsed
# 0.00 0.00 0.03
identical(result1, result2) && identical(result2, result3)
# [1] TRUE