Sergey Bombin
Sergey Bombin

Reputation: 45

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?

Upvotes: 2

Views: 847

Answers (2)

Jilber Urbina
Jilber Urbina

Reputation: 61214

Maybe something like this

[1]  TRUE  TRUE FALSE FALSE
> df[df[,1] <= 81 & 81 <=  df[,2], 3]
[1] FL91 FL12

Upvotes: 2

Pierre L
Pierre L

Reputation: 28461

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"

Upvotes: 3

Related Questions