A simple one lapplywill do the trick. Line identification is relatively simple. The if statement takes only a shorter interval, when several values ββwork, itβs a little harder to understand, but basically, if there are several possibilities, I take a line where the interval is equal to the smallest possible interval.
foo <- function(i) {
res <- data[data$lower < i & data$upper > i, ]
if (nrow(res) > 1) {
res <- res[which(res$upper - res$lower == min(res$upper - res$lower)), ]
}
if (nrow(res) == 0) return(NA)
return(res$importantval)
}
results <- data.frame(vals, sapply(vals, foo))
This assumes that there are no intervals of the same length. If possible, you can add return(min(res$importantval))at the end to get only a lower value.
, :
results <- lapply(vals, foo)
names(results) <- vals