在R中的两个日期时间之间创建间隔日期时间

时间:2017-08-17 21:34:33

标签: r datetime vector

我希望每隔15分钟创建一个所有日期和时间的矢量,时间为15/07/2017 13:45和29/07/2017 13:15。总的来说,应该有1343个值,包括上面的两个日期时间列表。

我怎样才能在R?

中这样做

2 个答案:

答案 0 :(得分:1)

您的数据

library(lubridate)
x <- parse_date_time(c("15/07/2017 13:45","29/07/2017 13:15"), "%d%m%y HM")
# "2017-07-15 13:45:00 UTC" "2017-07-29 13:15:00 UTC"

解决方案

seq(x[1], x[2], "15 min")
# "2017-07-15 13:45:00 UTC" "2017-07-15 14:00:00 UTC" "2017-07-15 14:15:00 UTC"

答案 1 :(得分:0)

试试这个:

library(lubridate)

startDate <- dmy_hm("15/07/2017 13:45")
endDate <- dmy_hm("29/07/2017 13:15")

myVector <- seq(from = startDate, to = endDate, by = 900) # 900 seconds = 15 minutes

> length(myVector)
[1] 1343

> head(myVector, 20)
 [1] "2017-07-15 13:45:00 UTC" "2017-07-15 14:00:00 UTC" "2017-07-15 14:15:00 UTC" "2017-07-15 14:30:00 UTC" "2017-07-15 14:45:00 UTC"
 [6] "2017-07-15 15:00:00 UTC" "2017-07-15 15:15:00 UTC" "2017-07-15 15:30:00 UTC" "2017-07-15 15:45:00 UTC" "2017-07-15 16:00:00 UTC"
[11] "2017-07-15 16:15:00 UTC" "2017-07-15 16:30:00 UTC" "2017-07-15 16:45:00 UTC" "2017-07-15 17:00:00 UTC" "2017-07-15 17:15:00 UTC"
[16] "2017-07-15 17:30:00 UTC" "2017-07-15 17:45:00 UTC" "2017-07-15 18:00:00 UTC" "2017-07-15 18:15:00 UTC" "2017-07-15 18:30:00 UTC"
> tail(myVector, 20)
 [1] "2017-07-29 08:30:00 UTC" "2017-07-29 08:45:00 UTC" "2017-07-29 09:00:00 UTC" "2017-07-29 09:15:00 UTC" "2017-07-29 09:30:00 UTC"
 [6] "2017-07-29 09:45:00 UTC" "2017-07-29 10:00:00 UTC" "2017-07-29 10:15:00 UTC" "2017-07-29 10:30:00 UTC" "2017-07-29 10:45:00 UTC"
[11] "2017-07-29 11:00:00 UTC" "2017-07-29 11:15:00 UTC" "2017-07-29 11:30:00 UTC" "2017-07-29 11:45:00 UTC" "2017-07-29 12:00:00 UTC"
[16] "2017-07-29 12:15:00 UTC" "2017-07-29 12:30:00 UTC" "2017-07-29 12:45:00 UTC" "2017-07-29 13:00:00 UTC" "2017-07-29 13:15:00 UTC