I have a sorted vector x and another vector y (not necessarily the same length). For each record y, I want to find the index of the largest record x, which is less than or equal to record y.
For example, if
x <- c(2,4,8,9,12)
y <- c(5,10)
I want to return indexes xfor each record y:
- 2 (because 4 is the largest record x is less than 5) and
- 4.
I can do this easily by going along y, but I want to know if there is a way to vectorize it. So is it possible to vectorize:
for (k in 1:length(y)){
max(which(x < y[k]))
}
source
share