Check interval contains number in R

dplanet picture dplanet · Mar 24, 2012 · Viewed 19.2k times · Source

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?

Answer

Prasad Chalasani picture Prasad Chalasani · Mar 25, 2012

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