2021年4月3日星期六

Time differences for multiple entries for same ID in R

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.

Any help would be greatly appreciated.

https://stackoverflow.com/questions/66937427/time-differences-for-multiple-entries-for-same-id-in-r April 04, 2021 at 10:04AM

没有评论:

发表评论