我正在尝试计算R中的时间间隔。我创建了一个列,用于减去条目和退出之间的差异。
我尝试使用mutate函数执行此操作:
library(tidyverse)
d1 <- d1 %>% mutate(Difference = (hms(d1$Exit) - hms(d1$Entry)))
但是我得到的结果是“7H -4M 0S”(减去4分钟)。
计算时差以便以后绘制成图表会有什么好处?
Date Entry Exit Difference
1 2018-03-16 08:06:00 16:29:00 8H 23M 0S
2 2018-05-28 08:08:00 13:30:00 5H 22M 0S
3 2018-04-30 08:44:00 15:40:00 7H -4M 0S
4 2018-05-22 08:52:00 15:30:00 7H -22M 0S
5 2018-05-18 08:54:00 15:33:00 7H -21M 0S
6 2018-05-29 08:58:00 16:02:00 8H -56M 0S
7 2018-06-05 09:01:00 09:02:00 1M 0S
8 2018-04-13 09:01:00 16:23:00 7H 22M 0S
9 2018-04-03 09:01:00 16:56:00 7H 55M 0S
10 2018-05-08 09:03:00 16:35:00 7H 32M 0S
11 2018-05-30 09:05:00 15:56:00 6H 51M 0S
12 2018-04-24 09:07:00 16:44:00 7H 37M 0S
13 2018-03-27 09:09:00 16:54:00 7H 45M 0S
14 2018-03-23 09:12:00 16:28:00 7H 16M 0S
15 2018-05-15 09:13:00 16:33:00 7H 20M 0S
16 2018-04-17 09:14:00 16:38:00 7H 24M 0S
17 2018-04-10 09:20:00 16:45:00 7H 25M 0S
18 2018-03-20 09:21:00 16:38:00 7H 17M 0S
19 2018-04-23 09:39:00 16:29:00 7H -10M 0S
20 2018-02-05 09:41:00 11:47:00 2H 6M 0S
21 2018-06-01 10:10:00 16:56:00 6H 46M 0S
22 2018-04-05 10:24:00 18:02:00 8H -22M 0S
我尝试使用此功能:
d1 <- d1 %>% mutate(Difference = interval(Exit,Entry))
但我收到了这个错误:
Error in mutate_impl(.data, dots) :
Evaluation error: character string is not in a standard unambiguous format.
正如Sotos推荐的那样,我尝试使用它:
difftime(as.POSIXct(df$Exit, format = '%H:%M:%S'), as.POSIXct(df$Entry, format = '%H:%M:%S'))
我得到了正确的结果