So I have a dataframe like this
DF1
ID timestamp value
1 2018-07-31 20:00:00 45555
1 2018-08-01 06:50:00 333
1 2018-08-01 07:00:00 322
2 2018-08-01 13:00:00 1222
2 2018-08-01 22:00:00 1111
3 2018-08-01 11:00:00 0
3 2018-08-02 08:00:00 22222
And another one like this
DF2
startTime endTime ID
2018-08-01 06:45:00 2018-08-01 09:49:00 1
2019-12-02 11:01:00 2019-12-02 11:02:00 2
2018-08-02 07:59:00 2018-08-02 08:50:00 2
2018-08-02 07:59:00 2018-08-02 08:50:00 3
So I want to create a new column using mutate
which mutates DF2 that tells me if any time in between the startTime
and endTime
appears in DF1 that matches the ID.
So in DF2 if we have a date like 8/2/2018 and the time span is 7:59AM - 8:50AM , if any timestamp within this interval inclusive (2018-08-02 7:59:00, 2018-08-02 8:00:00, 2018-08-02 8:01:00...) appears in DF1 then a variable in DF2 reads 1, else 0. This is only the case if the ID
matches. Does not matter how many times, atleast once is what matters.
Final version of DF2 should look like.
startTime endTime ID match
2018-08-01 06:45:00 2018-08-01 09:49:00 1 1
2019-12-02 11:01:00 2019-12-02 11:02:00 2 0
2018-08-02 07:59:00 2018-08-02 08:50:00 2 0
2018-08-02 07:59:00 2018-08-02 08:50:00 3 1
Ideally I would like to use BETWEEN()
and remain in the tidyverse.