8

Is there any way to create in R a 24 hour vector with 5 minutes time interval from scratch or from an integer format like this:

2355 which would correspond to 23:55

155 which would correspond to 1:55

and so on.

Basically what I want is a vector with from 00:00 to 23:55 so I can plot a graphic with data corresponding to each time interval.

IRTFM
  • 258,963
  • 21
  • 364
  • 487

3 Answers3

27

There is a seq.POSIXt function which has the nice property that the by argument will get parsed for "numeric interval" meaning of one of "sec", "min", "hour", "day", "DSTday", "week", "month", "quarter" or "year . Then, if you print the results with format(z, "%H%M", tz="GMT") it can appear as desired:

format( seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date()+1), by = "5 min"),
          "%H%M", tz="GMT")  # hours (00-23) and min (00-59) and no space
  [1] "0000" "0005" "0010" "0015" "0020" "0025" "0030" "0035" "0040" "0045" "0050"
 [12] "0055" "0100" "0105" "0110" "0115" "0120" "0125" "0130" "0135" "0140" "0145"
 [23] "0150" "0155" "0200" "0205" "0210" "0215" "0220" "0225" "0230" "0235" "0240"
 [34] "0245" "0250" "0255" "0300" "0305" "0310" "0315" "0320" "0325" "0330" "0335"
 [45] "0340" "0345"  snipped the rest.

Unless you are within 360/48 degrees of Greenwich (or is it Paris) you need to put in the tz="GMT" so that the offset for your timezone does not mess this up. Without that this produced a sequence starting at "1700" for me. You could assign the inner result to a name if you needed to keep it available in your workspace, but it would not be a character value but rather a POSIXct object (numeric mode with class defined methods for display and manipulation):

z <- seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date()+1), by = "5 min")
> z[1]
[1] "2014-09-09 17:00:00 PDT"
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Ok I get almost what I want. Since I am having the data in different days the plot will have the wrong hours in the x data. How can I turn around this? – André Miguel Monteiro Sep 10 '14 at 22:28
  • Not sure what is meant without a more complete test cas and description of what is expected. (Since you already accepted another answer I will take this as already answered.) – IRTFM Sep 10 '14 at 22:34
  • When you are plotting, you may want to use the original datetime data from teh `seq.POSIXct` call as input for the X-axis, but use this sort of result (from the `format` call for the tick labels. Would of course need to know what graphical paradigm was intended and have some sample data for full coding efforts. – IRTFM Apr 08 '20 at 20:29
9
> seq(ISOdatetime(2001,2,3,0,0,0), ISOdatetime(2001,2,4,0,0,0), by=(60*5))
  [1] "2001-02-03 00:00:00 PST" "2001-02-03 00:05:00 PST"
  [3] "2001-02-03 00:10:00 PST" "2001-02-03 00:15:00 PST"
  [5] "2001-02-03 00:20:00 PST" "2001-02-03 00:25:00 PST"
  [7] "2001-02-03 00:30:00 PST" "2001-02-03 00:35:00 PST"
  [9] "2001-02-03 00:40:00 PST" "2001-02-03 00:45:00 PST"
 [11] "2001-02-03 00:50:00 PST" "2001-02-03 00:55:00 PST"
 [13] "2001-02-03 01:00:00 PST" "2001-02-03 01:05:00 PST"
 [15] "2001-02-03 01:10:00 PST" "2001-02-03 01:15:00 PST"
 [17] "2001-02-03 01:20:00 PST" "2001-02-03 01:25:00 PST"
 [19] "2001-02-03 01:30:00 PST" "2001-02-03 01:35:00 PST"
 [21] "2001-02-03 01:40:00 PST" "2001-02-03 01:45:00 PST"
 [23] "2001-02-03 01:50:00 PST" "2001-02-03 01:55:00 PST"
 [25] "2001-02-03 02:00:00 PST" "2001-02-03 02:05:00 PST"
 [27] "2001-02-03 02:10:00 PST" "2001-02-03 02:15:00 PST"
 [29] "2001-02-03 02:20:00 PST" "2001-02-03 02:25:00 PST"
 [31] "2001-02-03 02:30:00 PST" "2001-02-03 02:35:00 PST"
 [33] "2001-02-03 02:40:00 PST" "2001-02-03 02:45:00 PST"
 [35] "2001-02-03 02:50:00 PST" "2001-02-03 02:55:00 PST"
 [37] "2001-02-03 03:00:00 PST" "2001-02-03 03:05:00 PST"
 [39] "2001-02-03 03:10:00 PST" "2001-02-03 03:15:00 PST"
 [41] "2001-02-03 03:20:00 PST" "2001-02-03 03:25:00 PST"
 [43] "2001-02-03 03:30:00 PST" "2001-02-03 03:35:00 PST"
 [45] "2001-02-03 03:40:00 PST" "2001-02-03 03:45:00 PST"
 [47] "2001-02-03 03:50:00 PST" "2001-02-03 03:55:00 PST"
 [49] "2001-02-03 04:00:00 PST" "2001-02-03 04:05:00 PST"
 [51] "2001-02-03 04:10:00 PST" "2001-02-03 04:15:00 PST"
 [53] "2001-02-03 04:20:00 PST" "2001-02-03 04:25:00 PST"
 [55] "2001-02-03 04:30:00 PST" "2001-02-03 04:35:00 PST"
 [57] "2001-02-03 04:40:00 PST" "2001-02-03 04:45:00 PST"
 [59] "2001-02-03 04:50:00 PST" "2001-02-03 04:55:00 PST"
 [61] "2001-02-03 05:00:00 PST" "2001-02-03 05:05:00 PST"
 [63] "2001-02-03 05:10:00 PST" "2001-02-03 05:15:00 PST"
 [65] "2001-02-03 05:20:00 PST" "2001-02-03 05:25:00 PST"
 [67] "2001-02-03 05:30:00 PST" "2001-02-03 05:35:00 PST"
 [69] "2001-02-03 05:40:00 PST" "2001-02-03 05:45:00 PST"
 [71] "2001-02-03 05:50:00 PST" "2001-02-03 05:55:00 PST"
 [73] "2001-02-03 06:00:00 PST" "2001-02-03 06:05:00 PST"
 [75] "2001-02-03 06:10:00 PST" "2001-02-03 06:15:00 PST"
 [77] "2001-02-03 06:20:00 PST" "2001-02-03 06:25:00 PST"
 [79] "2001-02-03 06:30:00 PST" "2001-02-03 06:35:00 PST"
 [81] "2001-02-03 06:40:00 PST" "2001-02-03 06:45:00 PST"
 [83] "2001-02-03 06:50:00 PST" "2001-02-03 06:55:00 PST"
 [85] "2001-02-03 07:00:00 PST" "2001-02-03 07:05:00 PST"
 [87] "2001-02-03 07:10:00 PST" "2001-02-03 07:15:00 PST"
 [89] "2001-02-03 07:20:00 PST" "2001-02-03 07:25:00 PST"
 [91] "2001-02-03 07:30:00 PST" "2001-02-03 07:35:00 PST"
 [93] "2001-02-03 07:40:00 PST" "2001-02-03 07:45:00 PST"
 [95] "2001-02-03 07:50:00 PST" "2001-02-03 07:55:00 PST"
 [97] "2001-02-03 08:00:00 PST" "2001-02-03 08:05:00 PST"
 [99] "2001-02-03 08:10:00 PST" "2001-02-03 08:15:00 PST"
[101] "2001-02-03 08:20:00 PST" "2001-02-03 08:25:00 PST"
[103] "2001-02-03 08:30:00 PST" "2001-02-03 08:35:00 PST"
[105] "2001-02-03 08:40:00 PST" "2001-02-03 08:45:00 PST"
[107] "2001-02-03 08:50:00 PST" "2001-02-03 08:55:00 PST"
[109] "2001-02-03 09:00:00 PST" "2001-02-03 09:05:00 PST"
[111] "2001-02-03 09:10:00 PST" "2001-02-03 09:15:00 PST"
[113] "2001-02-03 09:20:00 PST" "2001-02-03 09:25:00 PST"
[115] "2001-02-03 09:30:00 PST" "2001-02-03 09:35:00 PST"
[117] "2001-02-03 09:40:00 PST" "2001-02-03 09:45:00 PST"
[119] "2001-02-03 09:50:00 PST" "2001-02-03 09:55:00 PST"
[121] "2001-02-03 10:00:00 PST" "2001-02-03 10:05:00 PST"
[123] "2001-02-03 10:10:00 PST" "2001-02-03 10:15:00 PST"
[125] "2001-02-03 10:20:00 PST" "2001-02-03 10:25:00 PST"
[127] "2001-02-03 10:30:00 PST" "2001-02-03 10:35:00 PST"
[129] "2001-02-03 10:40:00 PST" "2001-02-03 10:45:00 PST"
[131] "2001-02-03 10:50:00 PST" "2001-02-03 10:55:00 PST"
[133] "2001-02-03 11:00:00 PST" "2001-02-03 11:05:00 PST"
[135] "2001-02-03 11:10:00 PST" "2001-02-03 11:15:00 PST"
[137] "2001-02-03 11:20:00 PST" "2001-02-03 11:25:00 PST"
[139] "2001-02-03 11:30:00 PST" "2001-02-03 11:35:00 PST"
[141] "2001-02-03 11:40:00 PST" "2001-02-03 11:45:00 PST"
[143] "2001-02-03 11:50:00 PST" "2001-02-03 11:55:00 PST"
[145] "2001-02-03 12:00:00 PST" "2001-02-03 12:05:00 PST"
[147] "2001-02-03 12:10:00 PST" "2001-02-03 12:15:00 PST"
[149] "2001-02-03 12:20:00 PST" "2001-02-03 12:25:00 PST"
[151] "2001-02-03 12:30:00 PST" "2001-02-03 12:35:00 PST"
[153] "2001-02-03 12:40:00 PST" "2001-02-03 12:45:00 PST"
[155] "2001-02-03 12:50:00 PST" "2001-02-03 12:55:00 PST"
[157] "2001-02-03 13:00:00 PST" "2001-02-03 13:05:00 PST"
[159] "2001-02-03 13:10:00 PST" "2001-02-03 13:15:00 PST"
[161] "2001-02-03 13:20:00 PST" "2001-02-03 13:25:00 PST"
[163] "2001-02-03 13:30:00 PST" "2001-02-03 13:35:00 PST"
[165] "2001-02-03 13:40:00 PST" "2001-02-03 13:45:00 PST"
[167] "2001-02-03 13:50:00 PST" "2001-02-03 13:55:00 PST"
[169] "2001-02-03 14:00:00 PST" "2001-02-03 14:05:00 PST"
[171] "2001-02-03 14:10:00 PST" "2001-02-03 14:15:00 PST"
[173] "2001-02-03 14:20:00 PST" "2001-02-03 14:25:00 PST"
[175] "2001-02-03 14:30:00 PST" "2001-02-03 14:35:00 PST"
[177] "2001-02-03 14:40:00 PST" "2001-02-03 14:45:00 PST"
[179] "2001-02-03 14:50:00 PST" "2001-02-03 14:55:00 PST"
[181] "2001-02-03 15:00:00 PST" "2001-02-03 15:05:00 PST"
[183] "2001-02-03 15:10:00 PST" "2001-02-03 15:15:00 PST"
[185] "2001-02-03 15:20:00 PST" "2001-02-03 15:25:00 PST"
[187] "2001-02-03 15:30:00 PST" "2001-02-03 15:35:00 PST"
[189] "2001-02-03 15:40:00 PST" "2001-02-03 15:45:00 PST"
[191] "2001-02-03 15:50:00 PST" "2001-02-03 15:55:00 PST"
[193] "2001-02-03 16:00:00 PST" "2001-02-03 16:05:00 PST"
[195] "2001-02-03 16:10:00 PST" "2001-02-03 16:15:00 PST"
[197] "2001-02-03 16:20:00 PST" "2001-02-03 16:25:00 PST"
[199] "2001-02-03 16:30:00 PST" "2001-02-03 16:35:00 PST"
[201] "2001-02-03 16:40:00 PST" "2001-02-03 16:45:00 PST"
[203] "2001-02-03 16:50:00 PST" "2001-02-03 16:55:00 PST"
[205] "2001-02-03 17:00:00 PST" "2001-02-03 17:05:00 PST"
[207] "2001-02-03 17:10:00 PST" "2001-02-03 17:15:00 PST"
[209] "2001-02-03 17:20:00 PST" "2001-02-03 17:25:00 PST"
[211] "2001-02-03 17:30:00 PST" "2001-02-03 17:35:00 PST"
[213] "2001-02-03 17:40:00 PST" "2001-02-03 17:45:00 PST"
[215] "2001-02-03 17:50:00 PST" "2001-02-03 17:55:00 PST"
[217] "2001-02-03 18:00:00 PST" "2001-02-03 18:05:00 PST"
[219] "2001-02-03 18:10:00 PST" "2001-02-03 18:15:00 PST"
[221] "2001-02-03 18:20:00 PST" "2001-02-03 18:25:00 PST"
[223] "2001-02-03 18:30:00 PST" "2001-02-03 18:35:00 PST"
[225] "2001-02-03 18:40:00 PST" "2001-02-03 18:45:00 PST"
[227] "2001-02-03 18:50:00 PST" "2001-02-03 18:55:00 PST"
[229] "2001-02-03 19:00:00 PST" "2001-02-03 19:05:00 PST"
[231] "2001-02-03 19:10:00 PST" "2001-02-03 19:15:00 PST"
[233] "2001-02-03 19:20:00 PST" "2001-02-03 19:25:00 PST"
[235] "2001-02-03 19:30:00 PST" "2001-02-03 19:35:00 PST"
[237] "2001-02-03 19:40:00 PST" "2001-02-03 19:45:00 PST"
[239] "2001-02-03 19:50:00 PST" "2001-02-03 19:55:00 PST"
[241] "2001-02-03 20:00:00 PST" "2001-02-03 20:05:00 PST"
[243] "2001-02-03 20:10:00 PST" "2001-02-03 20:15:00 PST"
[245] "2001-02-03 20:20:00 PST" "2001-02-03 20:25:00 PST"
[247] "2001-02-03 20:30:00 PST" "2001-02-03 20:35:00 PST"
[249] "2001-02-03 20:40:00 PST" "2001-02-03 20:45:00 PST"
[251] "2001-02-03 20:50:00 PST" "2001-02-03 20:55:00 PST"
[253] "2001-02-03 21:00:00 PST" "2001-02-03 21:05:00 PST"
[255] "2001-02-03 21:10:00 PST" "2001-02-03 21:15:00 PST"
[257] "2001-02-03 21:20:00 PST" "2001-02-03 21:25:00 PST"
[259] "2001-02-03 21:30:00 PST" "2001-02-03 21:35:00 PST"
[261] "2001-02-03 21:40:00 PST" "2001-02-03 21:45:00 PST"
[263] "2001-02-03 21:50:00 PST" "2001-02-03 21:55:00 PST"
[265] "2001-02-03 22:00:00 PST" "2001-02-03 22:05:00 PST"
[267] "2001-02-03 22:10:00 PST" "2001-02-03 22:15:00 PST"
[269] "2001-02-03 22:20:00 PST" "2001-02-03 22:25:00 PST"
[271] "2001-02-03 22:30:00 PST" "2001-02-03 22:35:00 PST"
[273] "2001-02-03 22:40:00 PST" "2001-02-03 22:45:00 PST"
[275] "2001-02-03 22:50:00 PST" "2001-02-03 22:55:00 PST"
[277] "2001-02-03 23:00:00 PST" "2001-02-03 23:05:00 PST"
[279] "2001-02-03 23:10:00 PST" "2001-02-03 23:15:00 PST"
[281] "2001-02-03 23:20:00 PST" "2001-02-03 23:25:00 PST"
[283] "2001-02-03 23:30:00 PST" "2001-02-03 23:35:00 PST"
[285] "2001-02-03 23:40:00 PST" "2001-02-03 23:45:00 PST"
[287] "2001-02-03 23:50:00 PST" "2001-02-03 23:55:00 PST"
[289] "2001-02-04 00:00:00 PST"
Loops
  • 101
  • 2
3

Try

outer(c(0, seq_len(23)), 
           seq(0, 55, 5), 
           function(x, y) paste0(sprintf("%02.f", x) , ":", sprintf("%02.f", y)))
David Arenburg
  • 91,361
  • 17
  • 137
  • 196