计算一天内两个 hh:mm 变量之间的时间差
Calculate time difference between two hh:mm variables over the span of a day
我在计算两个变量之间的时间差时遇到问题。正好是入睡和醒来之间的时间。我考虑过使用 chron 来这样做,这就是我的例子。
DF <- data.frame(time_start = c("20:00", "21:30", "22:00", "23:00", "00:30", "02:00", "04:00"),
time_end = c("03:00", "06:30", "07:00", "09:00", "5:30", "09:00", "10:00"))
library(chron)
DF$time_start <- paste0(DF$time_start, ":00")
DF$time_start <- chron(times. =DF$time_start)
DF$time_end <- paste0(DF$time_end, ":00")
DF$time_end <- chron(times. =DF$time_end)
DF$time_duration <- times(DF$time_end - DF$time_start)
使用 chron 格式化变量部分按预期工作。 time_end 的摘要看起来不错。另一方面,time_start 相当混乱。 time_duration 是十进制。
> summary(DF$time_start)
Min. 1st Qu. Median Mean 3rd Qu. Max.
00:30:00 03:00:00 20:00:00 13:17:09 21:45:00 23:00:00
> summary(DF$time_end)
Min. 1st Qu. Median Mean 3rd Qu. Max.
03:00:00 06:00:00 07:00:00 07:08:34 09:00:00 10:00:00
> summary(DF$time_duration)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-0.7083333 -0.6250000 -0.5833333 -0.2559524 0.2291667 0.2916667
我的问题:
如何获得 time_start 和
的合理摘要结果
如何以更易读的方式(例如小时)区分 time_start 和 time_end?提前感谢您的帮助!
这给出了正确的持续时间,以防 end_time
与 start_time
相同或隔天:
library(tidyverse)
DF <- data.frame(time_start = c("20:00", "21:30", "22:00", "23:00", "00:30", "02:00", "04:00"),
time_end = c("03:00", "06:30", "07:00", "09:00", "5:30", "09:00", "10:00")) %>%
mutate_at(vars(starts_with("time")), hm) %>%
mutate(time_duration = if_else(
time_end > time_start,
time_end - time_start,
hm("24:00") - time_start + time_end)
)
DF
time_start time_end time_duration
1 20H 0M 0S 3H 0M 0S 7H 0M 0S
2 21H 30M 0S 6H 30M 0S 9H 0M 0S
3 22H 0M 0S 7H 0M 0S 9H 0M 0S
4 23H 0M 0S 9H 0M 0S 10H 0M 0S
5 30M 0S 5H 30M 0S 5H 0M 0S
6 2H 0M 0S 9H 0M 0S 7H 0M 0S
7 4H 0M 0S 10H 0M 0S 6H 0M 0S
我在计算两个变量之间的时间差时遇到问题。正好是入睡和醒来之间的时间。我考虑过使用 chron 来这样做,这就是我的例子。
DF <- data.frame(time_start = c("20:00", "21:30", "22:00", "23:00", "00:30", "02:00", "04:00"),
time_end = c("03:00", "06:30", "07:00", "09:00", "5:30", "09:00", "10:00"))
library(chron)
DF$time_start <- paste0(DF$time_start, ":00")
DF$time_start <- chron(times. =DF$time_start)
DF$time_end <- paste0(DF$time_end, ":00")
DF$time_end <- chron(times. =DF$time_end)
DF$time_duration <- times(DF$time_end - DF$time_start)
使用 chron 格式化变量部分按预期工作。 time_end 的摘要看起来不错。另一方面,time_start 相当混乱。 time_duration 是十进制。
> summary(DF$time_start)
Min. 1st Qu. Median Mean 3rd Qu. Max.
00:30:00 03:00:00 20:00:00 13:17:09 21:45:00 23:00:00
> summary(DF$time_end)
Min. 1st Qu. Median Mean 3rd Qu. Max.
03:00:00 06:00:00 07:00:00 07:08:34 09:00:00 10:00:00
> summary(DF$time_duration)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-0.7083333 -0.6250000 -0.5833333 -0.2559524 0.2291667 0.2916667
我的问题:
如何获得 time_start 和
的合理摘要结果如何以更易读的方式(例如小时)区分 time_start 和 time_end?提前感谢您的帮助!
这给出了正确的持续时间,以防 end_time
与 start_time
相同或隔天:
library(tidyverse)
DF <- data.frame(time_start = c("20:00", "21:30", "22:00", "23:00", "00:30", "02:00", "04:00"),
time_end = c("03:00", "06:30", "07:00", "09:00", "5:30", "09:00", "10:00")) %>%
mutate_at(vars(starts_with("time")), hm) %>%
mutate(time_duration = if_else(
time_end > time_start,
time_end - time_start,
hm("24:00") - time_start + time_end)
)
DF
time_start time_end time_duration
1 20H 0M 0S 3H 0M 0S 7H 0M 0S
2 21H 30M 0S 6H 30M 0S 9H 0M 0S
3 22H 0M 0S 7H 0M 0S 9H 0M 0S
4 23H 0M 0S 9H 0M 0S 10H 0M 0S
5 30M 0S 5H 30M 0S 5H 0M 0S
6 2H 0M 0S 9H 0M 0S 7H 0M 0S
7 4H 0M 0S 10H 0M 0S 6H 0M 0S