如何计算 R 中给定时间点的持续时间

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

我正在寻找一个包或R code可以帮助计算多个主题的不同时间点的持续时间。

这是数据的样子

------------------------------------
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
-------------------------------------

我想要这个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  |
-----------------------------------------------------------------------------------

你知道可以帮助我获得 table 2

的软件包或方法吗

好了:

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)) 

为了处理时间和日期,lubridate 包非常流行,并且与 tidyverse 的其他部分(如上面 Gonzalo 的 dplyr)配合得很好。有许多函数可以将字符串转换为日期或时间,然后转换为可以求和的持续时间和周期。

这是您的案例示例,使用 hms()periods_to_seconds()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

作为秒数或持续时间的输出将以不同方式打印,但它们将总结并为您提供总时间长度。

> secs
[1]  601 9234  976

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

如果您需要以相同 HH:MM:SS 格式格式化最终总和,您可能需要做一些额外的技巧,如下所示: