I have an ordered vector of unique integers in R and I want to find the index of the element closest to but less than or equal to some value. For example, for the vector
Base R provides findInterval which implements a binary search:
findInterval(17, c(4, 8, 15, 16, 23, 42))
@Khashaa already mentioned this in a comment.