简体   繁体   English

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

[英]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 的tidyversedplyr的其他部分配合使用。 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.

相关问题 如何用R计算时间序列数据的持续时间? - How to calculate the duration in time series data with R? 计算给定时间段内的时间间隔的持续时间 - Calculate duration of a time interval within a given period 给定时间栏,如何在R中创建时间仓? - Given time column, how can I create time bins in R? 如何根据 R 中的持续时间(显示开始和结束时间,以及持续时间以小时和分钟为单位)计算最长日期 - how to calculate the longest date based on time duration (display begin and end time, and duration in hours and minutes) in R 如何计算R中具有相同值的连续变量的持续时间 - How to calculate time duration for the continuous variables having same value in R 计算 r 中低于阈值的持续时间 - Calculate duration of time under threshold in r 如何计算两个日期之间的持续时间 - How to calculate duration of time between two dates 如何使用 R 将用文字 (Xhours Xminutes) 写入的持续时间转换为数字? - How can I convert time duration written as' in words (Xhours Xminutes) to numbers using R? 如何使用Python计算2D点的动态时间规整距离? - How can I calculate the Dynamic Time Warping distance of 2D-Points with Python? 如何一次计算多个变量和时间点的百分比变化? - How can I calculate percentage changes of multiple variables and time points at once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM