For each ID that is the same, I would like to add an end time and then calculate the time differences between each entry for that user ID.
This is the code I have so far:
user <- user %>% group_by(user$userdata.user_id) %>% arrange(user$hours) %>% mutate(time.interval= user$hours - lag(user$hours, default = first(user$hours))) %>% mutate(time.interval = round(time.interval/86400, digits = 2))
I was trying to use the diff time() function, however since I am trying to calculate the time difference with a preset end date ('02-20-2020' = 7), I am unable to attain the following results:
id hours time.decimal time.interval
123 03:32:12 1.200 3.3 (4.5 - 1.2)
123 12:37:56 4.500 2.5 (7 - 4.5)
140 09:46:33 6.300 0.7 (7 - 6.3)
**Note: the above is an example of what I want to achieve. 7 in the time interval column is the time decimal version of the given end date.
Any help would be greatly appreciated.