简体   繁体   中英

How can I calculate time duration for given time points in R

I'm trying to find a package or R code that can help to calculate the time duration of different time points for multiple subjects.

This is what the data looks like

------------------------------------
SubjectID     | Task      |Duration
------------------------------------
A             |Cleaning   |0:10:01
A             |Cleaning   |2:33:54
A             |Carpeting  |0:16:16
A             |Carpeting  |0:19:23
A             |Painting   |0:20:16
B             |Cleaning   |1:45:60
B             |Carpeting  |0:15:01
B             |Painting   |1:15:10
B             |Painting   |0:15:60
C             |Carpeting  |1:16:16
C             |Cleaning   |0:20:16
C             |Painting   |0:30:10
-------------------------------------

I want to get this table

-----------------------------------------------------------------------------------
SubjectID |Number      |Number       |Number        |Total number   |Duration  |
          |of Cleaning |of Carpeting |of Painting   | of Tasks      |in hours  |
-----------------------------------------------------------------------------------
A         |  2         |      2      |      1       |    5          | 3:33:11  |
B         |  1         |      1      |      2       |    4          | 3:52:18  |
C         |  1         |      1      |      1       |    3          | 2:10:07  |
-----------------------------------------------------------------------------------

Do you you know a package or an approach that can help me to get the table 2

For dealing with times and dates, the lubridate package is pretty popular, and works well with other parts of the tidyverse like dplyr from Gonzalo above. There are a number of functions to convert strings to dates or times, and then to durations and periods that can be summed up.

Here's an example for your case, using hms() , periods_to_seconds() , and as.duration() .

library(tidyverse)

# Need to load lubridate explicitly, even though it's part of tidyverse
library(lubridate) 
duration_strings <- c("0:10:01", "2:33:54", "0:16:16")

# Convert strings to times, then from times to seconds.
secs <- period_to_seconds(hms(duration_strings))
secs

# Convert strings to times, and then to duration objects
durations <- as.duration(hms(duration_strings))
durations

The output as seconds or durations will print differently, but they will sum up and get you an overall total length of time either way.

> secs
[1]  601 9234  976

> durations
[1] "601s (~10.02 minutes)" "9234s (~2.56 hours)"   "976s (~16.27 minutes)"

If you need the final sum formatted in the same HH:MM:SS format, you might need to do some additional tricks, like shown here: Is it possible to print a duration with HH:MM:SS format?

There you go:

library(dplyr)
Data_pivot <- Data %>% group_by(SubjectID) %>% summarise(number = n()
                                                   ,cleaning = sum(case_when(Task == 'Cleaning' ~ 1 
                                                                         ,TRUE ~ 0))
                                                   ,Carpeting = sum(case_when(Task == 'Carpeting' ~ 1 
                                                                             ,TRUE ~ 0))
                                                   ,Painting = sum(case_when(Task == 'Painting' ~ 1 
                                                                            ,TRUE ~ 0))
                                                   ,duration = sum(Duration)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM