Borja
Borja

Reputation: 73

How to compare and obtain the overlap between two columns based on their respective timestamps

I have two dataframes with the exact same two columns each: a column with as.POSIXct time, and a column with a value for each timestamp.

Here they are:

DF1:

   TIME                          OBJECT
1        1899-12-31 00:00:00     formes aimantées
2        1899-12-31 00:00:00     dinette thés
3        1899-12-31 00:00:01     dinette thés
4        1899-12-31 00:00:01     formes aimantées
5        1899-12-31 00:00:02     dinette thés
8        1899-12-31 00:00:02     boite
...

And DF2:

TIME                            OBJECT
1        1899-12-31 00:00:00     formes aimantées
2        1899-12-31 00:00:01     indéfini
3        1899-12-31 00:00:01     dinette thés
4        1899-12-31 00:00:02     boite
5        1899-12-31 00:00:02     dinette thés
8        1899-12-31 00:00:03     indéfini
...

I would like to create a third dataset with Time and the overlap between df1 and df2, to highlight when the same object is simultaneously present on both sides. The ultimate goal is to create a plot like the following, specifically the third (inferior row).

Here a figure with the comparison of two time series, and the overlap between them in the third row

Thanks!

Upvotes: 0

Views: 38

Answers (1)

Eonema
Eonema

Reputation: 1201

dplyr::inner_join(DF1, DF2, by = join_by(TIME, OBJECT))

Upvotes: 2

Related Questions