Afiq Johari
Afiq Johari

Reputation: 1462

R Generate hourly datetime

I want to generate datetime hourly as below

Thank you

2017-01-01 0100
2017-01-01 0200
...
2020-01-01 2300
2020-01-31 2400

Upvotes: 1

Views: 324

Answers (2)

RyanFrost
RyanFrost

Reputation: 1428

You can also do this with lubridate:

library(lubridate)
seq(ymd_h("2017-01-01-00"), ymd_h("2020-01-31-24"), by = "hours")

Upvotes: 1

Waldi
Waldi

Reputation: 41230

as.POSIXct('2017-01-01')+3600*1:(24*31)

 [1] "2017-01-01 01:00:00 CET" "2017-01-01 02:00:00 CET" "2017-01-01 03:00:00 CET" "2017-01-01 04:00:00 CET"
 [5] "2017-01-01 05:00:00 CET" "2017-01-01 06:00:00 CET" "2017-01-01 07:00:00 CET" "2017-01-01 08:00:00 CET"
 [9] "2017-01-01 09:00:00 CET" "2017-01-01 10:00:00 CET" "2017-01-01 11:00:00 CET" "2017-01-01 12:00:00 CET"
[13] "2017-01-01 13:00:00 CET" "2017-01-01 14:00:00 CET" "2017-01-01 15:00:00 CET" "2017-01-01 16:00:00 CET"
[17] "2017-01-01 17:00:00 CET" "2017-01-01 18:00:00 CET" "2017-01-01 19:00:00 CET" "2017-01-01 20:00:00 CET"
[21] "2017-01-01 21:00:00 CET" "2017-01-01 22:00:00 CET" "2017-01-01 23:00:00 CET" "2017-01-02 00:00:00 CET"

Upvotes: 1

Related Questions