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:
df
A data frame containing the intervals to be
evaluated.lowLim
the column index or name in the data frame
df
corresponding to the lower limit of the interval.upLim
the column index or name in the data frame
df
corresponding to the upper limit of the interval.true.val
the true value to be checked if it falls
within the interval defined by lowLim
and
upLim
.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:
## [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.