lundi 4 mai 2020

Finding nearest time point with conditions

Suppose I have this dataset

jar_camp    jar campaign    start_date.time      end_date.time
1_pf1.1     1   pf1.1       2019-01-05 10:34:00  2019-01-05 10:41:00
2_pf1.1     2   pf1.1       2019-01-05 10:52:34  2019-01-05 11:03:00
3_pf1.1     3   pf1.1       2019-01-05 11:11:42  2019-01-05 11:21:00
1_pf2.1     1   pf2.1       2019-01-09 10:29:19  2019-01-09 10:39:35
2_pf2.1     2   pf2.1       2019-01-09 10:42:15  2019-01-09 10:53:10
3_pf2.1     3   pf2.1       2019-01-09 10:58:35  2019-01-09 11:10:40
1_pf3.1     1   pf3.1       2019-01-15 10:11:52  2019-01-15 10:22:38
2_pf3.1     2   pf3.1       2019-01-15 10:27:38  2019-01-15 10:38:56
3_pf3.1     3   pf3.1       2019-01-15 10:42:02  2019-01-15 10:52:41
1_pf4.1     1   pf4.1       2019-01-22 09:06:29  2019-01-22 09:17:05
2_pf4.1     2   pf4.1       2019-01-22 09:21:22  2019-01-22 09:31:56
3_pf4.1     3   pf4.1       2019-01-22 09:35:00  2019-01-22 09:47:26
1_pf5.1     2   pf5.1       2019-01-29 10:48:45  2019-01-29 10:59:09
2_pf5.1     3   pf5.1       2019-01-29 11:04:44  2019-01-29 11:18:24
20_pf5.1    1   pf5.1       2019-01-29 15:05:47  2019-01-29 15:16:50
1_pf8.1     1   pf8.1       2019-03-12 08:57:45  2019-03-12 09:07:55
2_pf8.1     2   pf8.1       2019-03-12 09:12:23  2019-03-12 09:22:50
3_pf8.1     3   pf8.1       2019-03-12 09:25:58  2019-03-12 09:36:29
1_pf9.1     1   pf9.1       2019-03-27 08:11:11  2019-03-27 08:21:31
2_pf9.1     2   pf9.1       2019-03-27 08:23:16  2019-03-27 08:33:55
3_pf9.1     3   pf9.1       2019-03-27 08:35:22  2019-03-27 08:46:13

and I for each "jar" (except for "campaign"=pf1.1) want to calculate the difference between the "start_date.time" and the nearest "end_date.time" that is < than the "start_data.time". How would I do that? I'm a newbie and primarily use tidyveres.

Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire