val_in_interval()

Introduction

Here we implement a function to evaluate if a given value falls within a specific interval. The \(n\) intervals should be input in a data frame structure and the value as a numeric variable. The function returns a numeric vector of \(n\) elements with an integer value for each interval of either \(0\) if the value is below the interval, \(1\) if it is inside it (with a rightmost open limit) and \(2\) if it is above the interval.

The val_in_interval() function takes the following parameters:

Example

First let’s define a data frame with the intervals 1-4, 5-9, 10-15 and 17-20:

intervals <- data.frame(lowLim = c(1, 5, 10, 17), upLim = c(4, 9, 15, 20)) # Creates a data frame with three intervals: 1 - 4, 5 - 9, 10 - 15 and 17 - 20.

intervals
##   lowLim upLim
## 1      1     4
## 2      5     9
## 3     10    15
## 4     17    20

Lets say we want to find in which interval the value \(11\) falls in:

true.val <- 11

index <- val_in_interval(intervals, "lowLim", "upLim", true.val)

index
## [1] 2 2 1 0

Here we can see that the value falls in the third interval (10 - 15) marked with a 1 in the output vector.