[英]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.我试图找到一个 package 或
R code
,可以帮助计算多个受试者的不同时间点的持续时间。
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你知道 package 还是可以帮助我获得表 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.为了处理时间和日期,
lubridate
package 非常受欢迎,并且可以很好地与上面 Gonzalo 的tidyverse
等dplyr
的其他部分配合使用。 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()
.这是您的案例的示例,使用 hms(
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
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. output 的秒数或持续时间会以不同的方式打印,但它们会总结并为您提供两种方式的总时间长度。
> 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?如果您需要以相同 HH:MM:SS 格式格式化的最终总和,您可能需要做一些额外的技巧,如下所示: Is it possible to print a duration with HH:MM:SS format?
There you go:你有 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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.