0
votes

I have 300 values of a time series with daily frequency, is it possible to create a ts object, with the specific dates of this data? (Note there are days when there was no measurement, for example 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

and in the xlab add the corresponding dates df$x

1
ts is normally used with monthly, quarterly or annual data. For daily data try zoo or xts packages. - G. Grothendieck

1 Answers

0
votes

Option 1: library(zoo)

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

Option 2: library(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