Hi all,
I have two datasets that look a bit like this:
df_1 <- data.frame(time_point = c(1, 2, 3, 4, 5, 6, 7, 8))
df_2 <- data.frame(stretch_id = c("stretch1", "stretch2", "stretch3"),
stretch_starts = c(1, 4, 6),
stretch_ends = c(3, 5, 9))
Dataframe 1 is a list of time points. Dataframe 2 contains information about these time points should be organised - where they fall in between a value for stretch_starts and stretch_ends, they belong to that stretch.
I would like to join these two sets of data to reflect this, so that it looks like this:
df_aim <- data.frame(stretch_id = c("stretch1", "stretch1", "stretch1", "stretch2", "stretch2", "stretch3", "stretch3", "stretch3"),
time_point = c(1, 2, 3, 4, 5, 6, 7, 8),
stretch_starts = c(1, 1, 1, 4, 4, 6, 6, 6),
stretch_ends = c(3, 3, 3, 5, 5, 9, 9, 9))
i.e., if a value for time_point is equal or greater than a value in stretch_starts, and less than a value in stretch_ends, it belongs to that stretch - I want to create a single dataframe with information about which stretch my time_points belong to.
Can anyone propose any suggestions as to how I could get to something like df_aim from df_1 and df_2?
Thanks in advance