I have the following data frame in R:
df <- data.frame(name = c('p1_start','p1_end','p2_start','p2_end','p1_start','p1_end','p2_start','p2_end','p1_start','p1_end','p2_start','p2_end','p1_start','p1_end','p2_start','p2_end'),
time = c(1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31),
target = c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2),
comb = c(0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1))
And another data frame:
data <- data.frame(time = c(2,5,8,14,14,20,21,26,28,28),
name = c('a','b','c','d','e','f','g','h','i','j'))
So, if we take a look at df
we could sort the data by target and combination and we will notice that there are basically "groups". For example for target=1
and comb=0
there are four entries p1_start,p1_end,p2_start,p2_end
and it is the same for all other target/comb combinations.
On the other side data
contains entries with time
being a timestamp.
Goal: I want to map the values from both data frames based on time
.
Example: The first entry of data
has time=2
meaning it happened between p1_start
,p1_end
so it should get the values target=1
and comb=0
mapped to the data
data frame.
Example 2: The entries of data
with time=14
happened between p2_start
,p2_end
so they should get the values target=1
and comb=1
mapped to the data
data frame.
Idea: I thought I iterate over df
by target
and comb
and for each combination of them check if there are rows in data
whose time is between. The second could be done with the following command:
data[which(data$time > p1_start & data$time < p2_end),]
once I get the rows it is easy to append the values. Problem: how could I do the iteration? I tried with the following:
df %>%
group_by(target, comb) %>%
print(data[which(data$time > df$p1_start & data$time < df$p2_end),])
But I am getting an error that time
has not been initialized