0

I have a large database (some data shown as example)

Check_In Ward_1
2019-01-01 00:05:18 2019-01-01 00:09:32
2019-01-04 23:53:21 2019-01-05 08:09:36
2019-01-07 00:15:40 2019-01-07 10:43:53

I want to calculate the elapsed time from Check_In to Ward_1 for each row to get so

Check_In Ward_1 Time_Elapsed
2019-01-01 00:05:18 2019-01-01 00:09:32 00:04:14
2019-01-04 23:53:21 2019-01-05 08:09:36 08:16:15
2019-01-07 00:15:40 2019-01-07 10:43:53 10:28:13
  • Have you tried https://stackoverflow.com/questions/25033585/calculating-time-difference-between-two-columns ? – Ronak Shah Apr 13 '21 at 14:34
  • You can read https://stackoverflow.com/questions/67063020/calculate-time-difference-in-the-same-group/67063168 – crestor Apr 13 '21 at 14:39

1 Answers1

0

You can use difftime :

df1$Time_Elapsed <- difftime(df1$Ward_1, df1$Check_In, units = 'mins')

If you want output in hours, minutes and second format you can use seconds_to_period from lubridate.

df1$Time_Elapsed <- lubridate::seconds_to_period(difftime(df1$Ward_1, df1$Check_In, units = 'secs'))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213