创建具有特定日期的时间序列

时间:2018-04-19 21:15:48

标签: r time-series

我有300个具有每日频率的时间序列值,是否可以使用此数据的特定日期创建ts object? (注意有几天没有测量,例如 2000-01-06

  set.seed(1)
    d <- sample(seq(as.Date('2000/01/01'), as.Date('2001/01/01'), by="day"), 300)
    df <- data.frame(x = d[order(d)], y = rnorm(300))
    head(df);tail(df)
               x           y
    1 2000-01-03  0.45018710
    2 2000-01-04 -0.01855983
    3 2000-01-05 -0.31806837
    4 2000-01-07 -0.92936215
    5 2000-01-08 -1.48746031
    6 2000-01-09 -1.07519230
                 x          y
    295 2000-12-27  0.2051629
    296 2000-12-28 -3.0080486
    297 2000-12-29 -1.3661119
    298 2000-12-30 -0.4241023
    299 2000-12-31  0.2368037
    300 2001-01-01 -2.3427231
    serie <- ts(df$y, frequency = 365.25) 
plot(serie)

enter image description here

并在xlab中添加相应的日期df$x

1 个答案:

答案 0 :(得分:0)

选项1:图书馆(动物园)

library(zoo)
plot(zoo(df$y, order.by = df$x))

选项2:库(xts)

library(xts)
serie <- xts(df$y, order.by = df$x)
head(serie);tail(df)
                  [,1]
2000-01-03  0.45018710
2000-01-04 -0.01855983
2000-01-05 -0.31806837
2000-01-07 -0.92936215
2000-01-08 -1.48746031
2000-01-09 -1.07519230
             x          y
295 2000-12-27  0.2051629
296 2000-12-28 -3.0080486
297 2000-12-29 -1.3661119
298 2000-12-30 -0.4241023
299 2000-12-31  0.2368037
300 2001-01-01 -2.3427231
plot(serie)

enter image description here