Check interval contains number in R

21,559

Solution 1

You can simply use the inequality operators directly on the matrix columns. So I would have simply done:

> cbind( mat[,1] <= 23.3 & mat[,2] >= 23.3 )

      [,1]
[1,]  TRUE
[2,]  TRUE
[3,] FALSE

Solution 2

 mat <- matrix(c(22.2,  25.5,
    23.1 , 25.9,
    23.4,  26.1), ncol=2, byrow=TRUE)
 trueval <- 23.3
 apply(mat, 1, findInterval, x=trueval)
#[1] 1 1 0
 which( apply(mat, 1, findInterval, x=trueval) == 1)
#[1] 1 2
  apply(mat, 1, findInterval, x=trueval) == 1
#[1]  TRUE  TRUE FALSE

Solution 3

Just for the record, this can also be easily achieved using between from the data.table package.

data.table::between(23.3, mat[, 1], mat[, 2])
## [1]  TRUE  TRUE FALSE
Share:
21,559
dplanet
Author by

dplanet

Updated on December 16, 2020

Comments

  • dplanet
    dplanet over 3 years

    In R I have the following matrix (each row represents a bootstrap 95% confidence interval generated from the same sample data):

           low   high
    [1,]   22.2  25.5
    [2,]   23.1  25.9
    [3,]   23.4  26.1
    ...
    

    I know the true population mean of the data, it's 23.3. So the first two include the true mean but the third does not.

    In R, I want to run a for loop i through nrow(matrix) times, each i checking whether or not the true population mean of the data is in that particular interval, then return a column vector of height nrow(matrix) of TRUE if the interval contains the true mean, and FALSE otherwise.

    How could I do this?