每隔30分钟创建一个时间序列

时间:2016-02-09 16:10:43

标签: r seq

我正在尝试以30分钟的间隔创建一个时间序列。我使用了以下命令,同时显示了输出:

ts = seq(as.POSIXct("2009-01-01 00:00"), as.POSIXct("2014-12-31 23:30"),by = "hour") 

"2010-02-21 12:00:00 EST" "2010-02-21 13:00:00 EST" "2010-02-21 14:00:00 EST"

当我将其更改为by ="min"时,它会变为每分钟。

如何每隔30分钟创建一个时间序列?

2 个答案:

答案 0 :(得分:1)

默认单位为秒。所以只需要1800秒即可获得30分钟。

ts = seq(as.POSIXct(" 2009-01-01 00:00"),as.POSIXct(" 2014-12-31 23:30"),by = 1800)

  

TS [1:20]    [1]" 2009-01-01 00:00:00 EST" " 2009-01-01 00:30:00 EST" " 2009-01-01 01:00:00 EST" " 2009-01-01 01:30:00 EST" " 2009-01-01 02:00:00 EST"    [6]" 2009-01-01 02:30:00 EST" " 2009-01-01 03:00:00 EST" " 2009-01-01 03:30:00 EST" " 2009-01-01 04:00:00 EST" " 2009-01-01 04:30:00 EST"   [11]" 2009-01-01 05:00:00 EST" " 2009-01-01 05:30:00 EST" " 2009-01-01 06:00:00 EST" " 2009-01-01 06:30:00 EST" " 2009-01-01 07:00:00 EST"   [16]" 2009-01-01 07:30:00 EST" " 2009-01-01 08:00:00 EST" " 2009-01-01 08:30:00 EST" " 2009-01-01 09:00:00 EST" " 2009-01-01 09:30:00 EST"

答案 1 :(得分:1)

您可以在by参数中指定分钟,并传递时区" UTC"正如阿德里安指出的那样。检查?seq.POSIXt以获取有关指定为字符串的by参数的更多详细信息:

  

一个字符串,包含" sec"," min"," hour"," day",   " DSTday","周","月","季"或"年"。这可以是任选的   前面有一个(正或负)整数和一个空格,或者后跟   by" s"。

ts <- seq(as.POSIXct("2017-01-01", tz = "UTC"),
    as.POSIXct("2017-01-02", tz = "UTC"),
    by = "30 min")
head(ts)

<强>输出

[1] "2017-01-01 00:00:00 UTC"
[2] "2017-01-01 00:30:00 UTC"
[3] "2017-01-01 01:00:00 UTC"
[4] "2017-01-01 01:30:00 UTC"
[5] "2017-01-01 02:00:00 UTC"
[6] "2017-01-01 02:30:00 UTC"