简体   繁体   中英

Find the largest element in a vector less than values in another vector in R

I have a sorted vector x and another vector y (not necessarily the same length). For each entry of y, I want to find the index of the largest entry of x less than or equal to the entry of y.

For example if

x <- c(2,4,8,9,12) 
y <- c(5,10)

I want to return indices of x for each entry of y :

  • 2 (because 4 is the largest entry of x less than 5) and
  • 4.

I can do this easily by looping over y, but I want to know if there is a way to vectorize this. So can I vectorize:

for (k in 1:length(y)){
    max(which(x < y[k]))
}

Assuming x is sorted, the findInterval function will work:

findInterval(y,x)
# 2 4

Using Vectorize:

x <- c(2,4,8,9,12) 
y <- c(5,10)

largest_less_than<-function(x,y){
  which(x == max(x[x < y]))
}

largest_less_than <- Vectorize(largest_less_than, vectorize.args = 'y')

largest_less_than(x = x, y = y)

If the vectors are sorted, you can use binary search. Check the middle value of x, and if y is smaller than that, go to the middle on the left side. Vice versa. Keep doing this and you will get the maximum value less than or equal. Then find the entry number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM