Sergey Bombin Sergey Bombin - 29 days ago 5
R Question

Find interval from data frame start and end points

I have the data frame (DF):

Start End Result
2 300 FL91
5 250 FL12
550 750 FL18
720 900 FL41


I want to find all "Results" that are between "Start" < 81 > "End" (So, I want to find all "Results" that lies on the interval with "Start and "End" points less than 81.
I tried:

y <- 81
findInterval(y, DF$Start, DF$End)


But I got such errors: 'vec' must be sorted non-decreasingly and not contain NAs.
I am sure that my data frame does not contain NA and is not sorted decreasingly.
I also tried to use suggestions from the similar topics but cannot figure out the solution.

Extracting data from data frame

Interval search on a data frame

So, what should I do?

Answer

You can use a ready-built function or create your own:

findInt <- function(value, start, end) {
  start < value & end > value
}

indx <- findInt(81, DF$Start, DF$End)
DF$Result[indx]
#[1] "FL91" "FL12"