在R中创建一个24小时的向量,时间间隔为5分钟

时间:2014-09-10 21:27:42

标签: r time

有没有办法在R中创建一个24小时的向量,从头开始有5分钟的时间间隔或者像这样的整数格式:

2355,相当于23:55

155,相当于1:55

等等。

基本上我想要的是一个从00:00到23:55的矢量,所以我可以绘制一个图形,其中包含与每个时间间隔相对应的数据。

3 个答案:

答案 0 :(得分:16)

有一个seq.POSIXt函数,它具有nice属性,by参数将被解析为"数字区间"含义。如果您使用format(z, "%H%M", tz="GMT")打印结果,则会显示所需内容:

format( seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date()+1), by = "5 min"),
          "%H%M", tz="GMT")
  [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.

除非你在格林威治的360/48度(或者是巴黎),否则你需要放入tz="GMT"以便不显示你所在时区的偏移量。如果没有这个,就会产生一个序列,从" 1700"为了我。您可以将内部结果分配给名称,如果您需要保留它,但它不是字符值,而是使用POSIXct对象:

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"

答案 1 :(得分:6)

> 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"

答案 2 :(得分:2)

尝试

outer(c(0, seq_len(23)), 
           seq(0, 55, 5), 
           function(x, y) paste0(sprintf("%02.f", x) , ":", sprintf("%02.f", y)))