hour()函数出问题了吗?

时间:2018-10-25 13:05:27

标签: r lubridate

我不明白为什么hour()使输出不合格。我尝试将时间间隔为15分钟(仅8到16)(我的意思是在8:00之后应该从8:15开始并在16:00结束),但是输出出错了。

从这里开始:

library(lubridate)

startDate = "2018-10-01"
endDate = "2018-10-03"

timeseq <- data.frame(DateTime = seq(from=as.POSIXct(startDate),to=as.POSIXct(endDate),by="15 min"))

timeseq[(hour(timeseq$DateTime)>8 & hour(timeseq$DateTime)<=16),]

我知道了:

> timeseq[(lubridate::hour(timeseq$DateTime)>8 & lubridate::hour(timeseq$DateTime)<=16),]
[1] "2018-10-01 09:00:00 GMT" "2018-10-01 09:15:00 GMT" "2018-10-01 09:30:00 GMT" "2018-10-01 09:45:00 GMT" "2018-10-01 10:00:00 GMT"
[6] "2018-10-01 10:15:00 GMT" "2018-10-01 10:30:00 GMT" "2018-10-01 10:45:00 GMT" "2018-10-01 11:00:00 GMT" "2018-10-01 11:15:00 GMT"
[11] "2018-10-01 11:30:00 GMT" "2018-10-01 11:45:00 GMT" "2018-10-01 12:00:00 GMT" "2018-10-01 12:15:00 GMT" "2018-10-01 12:30:00 GMT"
[16] "2018-10-01 12:45:00 GMT" "2018-10-01 13:00:00 GMT" "2018-10-01 13:15:00 GMT" "2018-10-01 13:30:00 GMT" "2018-10-01 13:45:00 GMT"
[21] "2018-10-01 14:00:00 GMT" "2018-10-01 14:15:00 GMT" "2018-10-01 14:30:00 GMT" "2018-10-01 14:45:00 GMT" "2018-10-01 15:00:00 GMT"
[26] "2018-10-01 15:15:00 GMT" "2018-10-01 15:30:00 GMT" "2018-10-01 15:45:00 GMT" "2018-10-01 16:00:00 GMT" "2018-10-01 16:15:00 GMT"
[31] "2018-10-01 16:30:00 GMT" "2018-10-01 16:45:00 GMT" "2018-10-02 09:00:00 GMT" "2018-10-02 09:15:00 GMT" "2018-10-02 09:30:00 GMT"
[36] "2018-10-02 09:45:00 GMT" "2018-10-02 10:00:00 GMT" "2018-10-02 10:15:00 GMT" "2018-10-02 10:30:00 GMT" "2018-10-02 10:45:00 GMT"
[41] "2018-10-02 11:00:00 GMT" "2018-10-02 11:15:00 GMT" "2018-10-02 11:30:00 GMT" "2018-10-02 11:45:00 GMT" "2018-10-02 12:00:00 GMT"
[46] "2018-10-02 12:15:00 GMT" "2018-10-02 12:30:00 GMT" "2018-10-02 12:45:00 GMT" "2018-10-02 13:00:00 GMT" "2018-10-02 13:15:00 GMT"
[51] "2018-10-02 13:30:00 GMT" "2018-10-02 13:45:00 GMT" "2018-10-02 14:00:00 GMT" "2018-10-02 14:15:00 GMT" "2018-10-02 14:30:00 GMT"
[56] "2018-10-02 14:45:00 GMT" "2018-10-02 15:00:00 GMT" "2018-10-02 15:15:00 GMT" "2018-10-02 15:30:00 GMT" "2018-10-02 15:45:00 GMT"
[61] "2018-10-02 16:00:00 GMT" "2018-10-02 16:15:00 GMT" "2018-10-02 16:30:00 GMT" "2018-10-02 16:45:00 GMT"

任何人都可以解释为什么以及如何获得正确的结果吗?

2 个答案:

答案 0 :(得分:0)

喜欢吗?

 library(lubridate)
    timeseq%>%
      mutate(time=substr(DateTime,12,20))%>%
      filter(as.numeric(hms(time))>=32400, as.numeric(hms(time))<=57600)%>%
      select(-time)

答案 1 :(得分:0)

hour没什么问题,您看到的输出是正确的。例如,提取小时16:30:00将返回满足条件16的{​​{1}}。从<= 16一直到16:15:00都是如此。

我假设您在基数R中表示类似的意思?

16:59:59