Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206316

Calculate time duration in seconds of single Date column, while group_by ID in R

$
0
0

Is there a way for R to group by ID, identify a 'break' in time, set this to 0, and then calculate time difference?

For instance:

                ID               TIME              
                A                12/18/2019 4:45:10 AM
                A                12/18/2019 4:45:11 AM
                A                12/18/2019 9:06:59 PM               
                B                12/18/2019 4:14:13 AM
                B                12/18/2019 4:14:14 AM

Does anyone know of a way to find the time duration for A? Notice this is not a difftime problem. I performed a certain activity at 4:45:10 am, then again at 4:45:11 am. I then stopped this activity, and picked back up at 9:06pm. Is there code that can accurately group IDs, and then calculate time difference in seconds whilst setting a huge gap in the time to ‘0’ to avoid inaccurate values?

This is not the correct solution.

                       diff<- data %>%
                       mutate(diff = difftime(as.POSIXct(Endtime, format = "%m/%d/%Y %I:%M:%S %p"), 
                       as.POSIXct(Starttime, format = "%m/%d/%Y %I:%M:%S %p"), units = "secs"))

Outcome:

                ID               TIME                         Duration   
                A                12/18/2019 4:45:10 AM        1 sec
                A                12/18/2019 4:45:11 AM        
                A                12/18/2019 9:06:59 PM        0 sec                    
                B                12/18/2019 4:14:13 AM        1 sec
                B                12/18/2019 4:14:14 AM

Any help is greatly appreciated. I will continue to research this. Thank you


Viewing all articles
Browse latest Browse all 206316

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>