The check interval contains a number in R

In R, I have the following matrix (each row represents a 95% confidence boot 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 value of a data population, it is 23.3. So the first two include the true average, but the third does not.

In R, I want to run a for loop from i to nrow(matrix) , each i , checking if the value of the true data set is actually in this particular interval, and then returns the column height vector nrow(matrix) of TRUE if the interval contains a true average value, and FALSE otherwise.

How can i do this?

+4
source share
4 answers

You can simply use the inequality operators directly on the columns of the matrix. So I would just do:

 > cbind( mat[,1] <= 23.3 & mat[,2] >= 23.3 ) [,1] [1,] TRUE [2,] TRUE [3,] FALSE 
+12
source
  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 
+6
source

For the record only, 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 
+5
source

There is a quick way to do this if your data is centered at zero,

 Zero.included = apply(mat,1,function(x){prod(sign(x))<=0}) 

if your matrix is ​​not centered around the middle just add new.mat=mat-23.3

0
source

Source: https://habr.com/ru/post/1403275/


All Articles