1

我正在尝试找到一个包或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
-------------------------------------

我想要这张桌子

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

你知道可以帮助我获得表2的包或方法吗

4

2 回答 2

2

对于处理时间和日期,该lubridate软件包非常受欢迎,并且可以与上面 Gonzalo中的其他tidyverse类似部分很好地配合使用。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 格式格式化的最终总和,您可能需要做一些额外的技巧,如下所示:Is it possible to print a duration with HH:MM:SS format?

于 2019-11-14T18:40:54.590 回答
1

你去:

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)) 
于 2019-11-14T18:07:53.527 回答