如何在 R 中按分钟创建带有日期的时间序列?


要使用日期按分钟创建时间序列,我们可以使用 seq 函数,并使用 as.POSIXct 定义日期和时间。例如,如果我们要生成一个时间序列,介于 2020 年 10 月 10 日下午 2 点到同一天下午 4 点,那么我们可以使用以下代码:

seq(from=as.POSIXct("2020-10-10 14:00"),to=as.POSIXct("2020-10-10 16:00"),by="min")

示例

 实时演示

seq(from=as.POSIXct("2020-01-01 00:01"),to=as.POSIXct("2020-01-01 01:00"),by="min")

输出

[1] "2020-01-01 00:01:00 IST" "2020-01-01 00:02:00 IST"
[3] "2020-01-01 00:03:00 IST" "2020-01-01 00:04:00 IST"
[5] "2020-01-01 00:05:00 IST" "2020-01-01 00:06:00 IST"
[7] "2020-01-01 00:07:00 IST" "2020-01-01 00:08:00 IST"
[9] "2020-01-01 00:09:00 IST" "2020-01-01 00:10:00 IST"
[11] "2020-01-01 00:11:00 IST" "2020-01-01 00:12:00 IST"
[13] "2020-01-01 00:13:00 IST" "2020-01-01 00:14:00 IST"
[15] "2020-01-01 00:15:00 IST" "2020-01-01 00:16:00 IST"
[17] "2020-01-01 00:17:00 IST" "2020-01-01 00:18:00 IST"
[19] "2020-01-01 00:19:00 IST" "2020-01-01 00:20:00 IST"
[21] "2020-01-01 00:21:00 IST" "2020-01-01 00:22:00 IST"
[23] "2020-01-01 00:23:00 IST" "2020-01-01 00:24:00 IST"
[25] "2020-01-01 00:25:00 IST" "2020-01-01 00:26:00 IST"
[27] "2020-01-01 00:27:00 IST" "2020-01-01 00:28:00 IST"
[29] "2020-01-01 00:29:00 IST" "2020-01-01 00:30:00 IST"
[31] "2020-01-01 00:31:00 IST" "2020-01-01 00:32:00 IST"
[33] "2020-01-01 00:33:00 IST" "2020-01-01 00:34:00 IST"
[35] "2020-01-01 00:35:00 IST" "2020-01-01 00:36:00 IST"
[37] "2020-01-01 00:37:00 IST" "2020-01-01 00:38:00 IST"
[39] "2020-01-01 00:39:00 IST" "2020-01-01 00:40:00 IST"
[41] "2020-01-01 00:41:00 IST" "2020-01-01 00:42:00 IST"
[43] "2020-01-01 00:43:00 IST" "2020-01-01 00:44:00 IST"
[45] "2020-01-01 00:45:00 IST" "2020-01-01 00:46:00 IST"
[47] "2020-01-01 00:47:00 IST" "2020-01-01 00:48:00 IST"
[49] "2020-01-01 00:49:00 IST" "2020-01-01 00:50:00 IST"
[51] "2020-01-01 00:51:00 IST" "2020-01-01 00:52:00 IST"
[53] "2020-01-01 00:53:00 IST" "2020-01-01 00:54:00 IST"
[55] "2020-01-01 00:55:00 IST" "2020-01-01 00:56:00 IST"
[57] "2020-01-01 00:57:00 IST" "2020-01-01 00:58:00 IST"
[59] "2020-01-01 00:59:00 IST" "2020-01-01 01:00:00 IST"

示例

 实时演示

seq(from=as.POSIXct("2020-06-05 00:10"),to=as.POSIXct("2020-06-05 00:50"),by="min")

输出

[1] "2020-06-05 00:10:00 IST" "2020-06-05 00:11:00 IST"
[3] "2020-06-05 00:12:00 IST" "2020-06-05 00:13:00 IST"
[5] "2020-06-05 00:14:00 IST" "2020-06-05 00:15:00 IST"
[7] "2020-06-05 00:16:00 IST" "2020-06-05 00:17:00 IST"
[9] "2020-06-05 00:18:00 IST" "2020-06-05 00:19:00 IST"
[11] "2020-06-05 00:20:00 IST" "2020-06-05 00:21:00 IST"
[13] "2020-06-05 00:22:00 IST" "2020-06-05 00:23:00 IST"
[15] "2020-06-05 00:24:00 IST" "2020-06-05 00:25:00 IST"
[17] "2020-06-05 00:26:00 IST" "2020-06-05 00:27:00 IST"
[19] "2020-06-05 00:28:00 IST" "2020-06-05 00:29:00 IST"
[21] "2020-06-05 00:30:00 IST" "2020-06-05 00:31:00 IST"
[23] "2020-06-05 00:32:00 IST" "2020-06-05 00:33:00 IST"
[25] "2020-06-05 00:34:00 IST" "2020-06-05 00:35:00 IST"
[27] "2020-06-05 00:36:00 IST" "2020-06-05 00:37:00 IST"
[29] "2020-06-05 00:38:00 IST" "2020-06-05 00:39:00 IST"
[31] "2020-06-05 00:40:00 IST" "2020-06-05 00:41:00 IST"
[33] "2020-06-05 00:42:00 IST" "2020-06-05 00:43:00 IST"
[35] "2020-06-05 00:44:00 IST" "2020-06-05 00:45:00 IST"
[37] "2020-06-05 00:46:00 IST" "2020-06-05 00:47:00 IST"
[39] "2020-06-05 00:48:00 IST" "2020-06-05 00:49:00 IST"
[41] "2020-06-05 00:50:00 IST"

示例

我们可以使用 tz 参数在不同时区中创建这些序列,如下所示:

 实时演示

> seq(from=as.POSIXct("2020-06-05 00:10",tz="UTC"),to=as.POSIXct("2020-06-05 00:50",tz="UTC"),by="min")

输出

[1] "2020-06-05 00:10:00 UTC" "2020-06-05 00:11:00 UTC"
[3] "2020-06-05 00:12:00 UTC" "2020-06-05 00:13:00 UTC"
[5] "2020-06-05 00:14:00 UTC" "2020-06-05 00:15:00 UTC"
[7] "2020-06-05 00:16:00 UTC" "2020-06-05 00:17:00 UTC"
[9] "2020-06-05 00:18:00 UTC" "2020-06-05 00:19:00 UTC"
[11] "2020-06-05 00:20:00 UTC" "2020-06-05 00:21:00 UTC"
[13] "2020-06-05 00:22:00 UTC" "2020-06-05 00:23:00 UTC"
[15] "2020-06-05 00:24:00 UTC" "2020-06-05 00:25:00 UTC"
[17] "2020-06-05 00:26:00 UTC" "2020-06-05 00:27:00 UTC"
[19] "2020-06-05 00:28:00 UTC" "2020-06-05 00:29:00 UTC"
[21] "2020-06-05 00:30:00 UTC" "2020-06-05 00:31:00 UTC"
[23] "2020-06-05 00:32:00 UTC" "2020-06-05 00:33:00 UTC"
[25] "2020-06-05 00:34:00 UTC" "2020-06-05 00:35:00 UTC"
[27] "2020-06-05 00:36:00 UTC" "2020-06-05 00:37:00 UTC"
[29] "2020-06-05 00:38:00 UTC" "2020-06-05 00:39:00 UTC"
[31] "2020-06-05 00:40:00 UTC" "2020-06-05 00:41:00 UTC"
[33] "2020-06-05 00:42:00 UTC" "2020-06-05 00:43:00 UTC"
[35] "2020-06-05 00:44:00 UTC" "2020-06-05 00:45:00 UTC"
[37] "2020-06-05 00:46:00 UTC" "2020-06-05 00:47:00 UTC"
[39] "2020-06-05 00:48:00 UTC" "2020-06-05 00:49:00 UTC"
[41] "2020-06-05 00:50:00 UTC"

示例

 实时演示

> seq(from=as.POSIXct("2020-06-05 00:10",tz="GMT"),to=as.POSIXct("2020-06-05 00:50",tz="GMT"),by="min")

输出

[1] "2020-06-05 00:10:00 GMT" "2020-06-05 00:11:00 GMT"
[3] "2020-06-05 00:12:00 GMT" "2020-06-05 00:13:00 GMT"
[5] "2020-06-05 00:14:00 GMT" "2020-06-05 00:15:00 GMT"
[7] "2020-06-05 00:16:00 GMT" "2020-06-05 00:17:00 GMT"
[9] "2020-06-05 00:18:00 GMT" "2020-06-05 00:19:00 GMT"
[11] "2020-06-05 00:20:00 GMT" "2020-06-05 00:21:00 GMT"
[13] "2020-06-05 00:22:00 GMT" "2020-06-05 00:23:00 GMT"
[15] "2020-06-05 00:24:00 GMT" "2020-06-05 00:25:00 GMT"
[17] "2020-06-05 00:26:00 GMT" "2020-06-05 00:27:00 GMT"
[19] "2020-06-05 00:28:00 GMT" "2020-06-05 00:29:00 GMT"
[21] "2020-06-05 00:30:00 GMT" "2020-06-05 00:31:00 GMT"
[23] "2020-06-05 00:32:00 GMT" "2020-06-05 00:33:00 GMT"
[25] "2020-06-05 00:34:00 GMT" "2020-06-05 00:35:00 GMT"
[27] "2020-06-05 00:36:00 GMT" "2020-06-05 00:37:00 GMT"
[29] "2020-06-05 00:38:00 GMT" "2020-06-05 00:39:00 GMT"
[31] "2020-06-05 00:40:00 GMT" "2020-06-05 00:41:00 GMT"
[33] "2020-06-05 00:42:00 GMT" "2020-06-05 00:43:00 GMT"
[35] "2020-06-05 00:44:00 GMT" "2020-06-05 00:45:00 GMT"
[37] "2020-06-05 00:46:00 GMT" "2020-06-05 00:47:00 GMT"
[39] "2020-06-05 00:48:00 GMT" "2020-06-05 00:49:00 GMT"
[41] "2020-06-05 00:50:00 GMT"

更新于:2020-10-17

1K+ 次浏览

开启你的职业生涯

完成课程,获得认证

开始
广告