R findInterval Function


findInterval(x,vec) function finds the indices of x in vec.

findInterval(x, vec, rightmost.closed = FALSE, all.inside = FALSE)

x: number
vec: numeric vector
rightmost.closed: logical; if true, the rightmost interval, vec[N-1] .. vec[N] is treated as closed
all.inside: logical; if true, the returned indices are coerced into 1,...,N-1, i.e., 0 is mapped to 1 and N to N-1


> v <- c(1:10)
> findInterval(3,v)
[1] 3

> v <- c(3,5,9,2,5,32,11)
> findInterval(9,v)
Error in findInterval(9, v) : 'vec' must be sorted non-decreasingly

> v2 <- sort(v)
> v2

[1]  2  3  5  5  9 11 32

> findInterval(9,v2)
[1] 5

> findInterval(5,v2)
[1] 4