代码之家  ›  专栏  ›  技术社区  ›  Rafael Díaz

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

  •  0
  • Rafael Díaz  · 技术社区  · 8 年前

    我有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 回复  |  直到 8 年前
        1
  •  0
  •   Rafael Díaz    8 年前

    选项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