Suppose I have two vectors of values:
a <- c(1,3,4,5,6,7,3)
b <- c(3,5,1,3,2)
And I want to apply some function, FUN
, to each of the inputs of a
against the whole of b
, what's the most efficient way to do it.
More specifically, in this case for each of the elements in a
I want to know for each value of 'a', how many of the elements in b
are greater than or equal to that value. The naïve approach is to do the following:
sum(a < b)
Of course, this doesn't work as it attempts to iterate over each of the vectors in parallel and gives me the warning:
longer object length is not a multiple of shorter object length
The output, btw, of that command is 3
.
However, in my situation, what I'd like to see is an output that is:
0 2 4 4 5 5 2
Of course, I realize I can do it using a for loop as such:
out <- c()
for (i in a) {
for (i in a) { out[length(out) + 1] = sum(b<i)}
}
Likewise, I could use sapply
as such:
sapply(a, function(x)sum(b<x))
However, I'm trying to be a good R programmer and stay away from for loops and sapply
seems to be very slow. Are there other alternatives?
For what it's worth, I'm doing this a couple of million times where length(b)
is always less than length(a)
and length(a)
ranges from 1 to 30.
a,b
vary in each of your million iterations, or is one of them fixed? - Prasad Chalasani