Rilcon42 Rilcon42 - 3 months ago 7
R Question

Find row that matches a range of values

I am trying to get the column

importantval
for a number that is within a range. I have no clue how to even start this, anyone have any ideas?

data<-data.frame(lower=c(1,4,6,7,7),upper=c(3,5,7,8,9),importantval=c(99,98,97,96,95))
vals<-c(1.14,3.5,7.2,19)

> data
lower upper importantval
1 1 3 99
2 4 5 98
3 6 7 97
4 7 8 96
5 7 9 95


output goal

# 1.14 99
# 3.5 98
# 7.2 96 <--return the smalller interval (from 7 to 8 is smaller than 7 to 9)
# 19 NA <--doesnt exist so return NA

Answer

A simple lapply would do the trick. Identifying the line is relatively easy. The if statement to take only the smaller interval when multiple values work is a bit harder to understand but mostly, if there are more than one possibility, I take the row where the interval is equal to the smallest interval possible.

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 that are of same length. If this is a possibility, you could add return(min(res$importantval)) at the end to get only the smaller value.

If you would want to keep both values, take the results in a list:

results <- lapply(vals, foo)
names(results) <- vals