Examples for 'base::findInterval'


Find Interval Numbers or Indices

Aliases: findInterval

Keywords: arith utilities

### ** Examples

x <- 2:18
v <- c(5, 10, 15) # create two bins [5,10) and [10,15)
cbind(x, findInterval(x, v))
       x  
 [1,]  2 0
 [2,]  3 0
 [3,]  4 0
 [4,]  5 1
 [5,]  6 1
 [6,]  7 1
 [7,]  8 1
 [8,]  9 1
 [9,] 10 2
[10,] 11 2
[11,] 12 2
[12,] 13 2
[13,] 14 2
[14,] 15 3
[15,] 16 3
[16,] 17 3
[17,] 18 3
N <- 100
X <- sort(round(stats::rt(N, df = 2), 2))
tt <- c(-100, seq(-2, 2, length.out = 201), +100)
it <- findInterval(tt, X)
tt[it < 1 | it >= N] # only first and last are outside range(X)
[1] -100  100
##  'left.open = TRUE' means  "mirroring" :
N <- length(v)
stopifnot(identical(
                  findInterval( x,  v,  left.open=TRUE) ,
              N - findInterval(-x, -v[N:1])))

[Package base version 4.2.3 Index]