使用r考虑午夜时间的时差计算

时间:2019-05-22 06:56:10

标签: r

我正在解决一个问题,我需要以分钟为单位计算时间差。我在一栏中有hh:mm:ss格式的时间值(超过28,000个值)。

我一直在使用以下函数来计算时间差。

tdiff <- dt[dt, Time_Diff := c(abs(diff(as.numeric(Time))),0.30), Student_ID]

其中dt -->是有序数据表,而 0.30 --> 30分钟分配给学生在课程中的最后一次活动。

这有效,但是没有考虑午夜时间。

感谢@niko的帮助,此问题已解决,但是仍未完成应分配给每个学生的上一次活动的“ 30分钟”。对此方向的任何帮助将不胜感激。谢谢。

期望的输出描述如下

S_Id    Date            Time           Time_Diff      Time_Diff(minutes)
A       10/08/2018      23:49:00       00:01:00       1 minutes 
A       10/08/2018      23:50:00       00:09:00       9
A       10/08/2018      23:59:00       00:02:00       2
A       10/09/2018      00:01:00       00:09:00       9
A       10/09/2018      00:10:00       08:02:00       482
A       10/09/2018      08:12:00       04:08:00       248
A       10/09/2018      12:20:00       10:01:00       601
A       10/09/2018      22:21:00       01:35:00       95
A       10/09/2018      23:56:00       00:09:00       9
A       10/10/2018      00:05:00       00:05:00       5
A       10/10/2018      00:10:00       00:02:00       2
A       10/10/2018      00:12:00       00:30:00       30
B       10/08/2018      23:49:00       00:01:00       1
B       10/08/2018      23:50:00       00:09:00       9
B       10/08/2018      23:59:00       00:02:00       2
B       10/09/2018      00:01:00       00:09:00       9
B       10/09/2018      00:10:00       08:02:00       482
B       10/09/2018      08:12:00       04:08:00       248
B       10/09/2018      12:20:00       10:01:00       601
B       10/09/2018      22:21:00       01:35:00       95
B       10/09/2018      23:56:00       00:09:00       9
B       10/10/2018      00:05:00       00:05:00       5
B       10/10/2018      00:10:00       00:02:00       2
B       10/10/2018      00:12:00       00:30:00       30
C       10/08/2018      23:49:00       00:01:00       1
C       10/08/2018      23:50:00       00:09:00       9
C       10/08/2018      23:59:00       00:02:00       2
C       10/09/2018      00:01:00       00:09:00       9
C       10/09/2018      00:10:00       08:02:00       482
C       10/09/2018      08:12:00       04:08:00       248
C       10/09/2018      12:20:00       10:01:00       601
C       10/09/2018      22:21:00       01:35:00       95
C       10/09/2018      23:56:00       00:09:00       9
C       10/10/2018      00:05:00       00:05:00       5
C       10/10/2018      00:10:00       00:02:00       2
C       10/10/2018      00:12:00       00:30:00       30

1 个答案:

答案 0 :(得分:0)

尝试将datetime转换为POSIXct

# dt is your data frame
diff(as.POSIXct(paste(dt$Date, dt$Time), format='%m/%d/%Y %H:%M:%S')) # or '%d/%m/%Y %H:%M:%S'

应该可以解决问题。

数据

dt <- structure(list(Date = c("10/08/2018", "10/08/2018", "10/08/2018", "10/09/2018", "10/09/2018", 
                              "10/09/2018", "10/09/2018", "10/09/2018", "10/09/2018", "10/10/2018", 
                              "10/10/2018", "10/10/2018"), 
                 Time = c("23:49:00", "23:50:00", "23:59:00", "00:01:00", "00:10:00", "08:12:00", 
                          "12:20:00", "22:21:00", "23:56:00", "00:05:00", "00:10:00", "00:12:00")), 
            class = "data.frame", row.names = c(NA, -12L))