R: subset a data frame based on conditions from another data frame
Here is a problem I am trying to solve. Say, I have two data frames like the following: observations <- data.frame(id = rep(rep(c(1,2,3,4), each=5), 5), time = c(rep(1:5,4), rep(6:10,4), rep(11:15,4), rep(16:20,4), rep(21:25,4)), measurement = rnorm(100,5,7)) sampletimes <- data.frame(location = letters[1:20], id = rep(1:4,5), time1 = rep(c(2,7,12,17,22), each=4), time2 = rep(c(4,9,14,19,24), each=4)) They both contain a column named id , which links the data frames. I want to have the measurement s from observationss for which time is between time1 and time2 from the sampletimes data frame.