I have a data.frame and a vector like:
df = data.frame(id = 1:3,
start = c(1, 1000, 16000),
end = c(100, 1100, 16100),
info = c("a", "b", "c"))
vec = cbind(id= 1:150, pos=c(sample(1:100, 50),
sample(1000:1100, 50),
sample(1600:16100, 50)))
For every value of vec
I want to find the corresponding row in df
where:
vec$pos >= df$start
vec$pos <= df$end
vec$id == df$id
So I can extract the info
column.
The problem is that df
is 1000 rows long and vec
is 2 million values long. Therefore looping over vec using sapply is slow. Can anyone do it by looping over df
instead?