Time Series Data in R

I. Ozkan

March 2025

Time Series Data

ts

ts(data = NA, start = 1, end = numeric(), frequency = 1,
   deltat = 1, ts.eps = getOption("ts.eps"),
   class = if(nseries > 1) c("mts", "ts", "matrix", "array") else "ts",
   names = )

Most frequently used arguments:

data: a vector/matrix of the observed values
start: the time of the observation
end: the time of the last observation
frequency: the number of observations per unit of time

Example

ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959
     Qtr1 Qtr2 Qtr3 Qtr4
1959         1    2    3
1960    4    5    6    7
1961    8    9   10     
x <- ts(1:10, frequency = 4, start = c(1959, 2)) # 2nd Quarter of 1959
str(x)
 Time-Series [1:10] from 1959 to 1962: 1 2 3 4 5 6 7 8 9 10
tsp(x)
[1] 1959.25 1961.50    4.00
start(x)
[1] 1959    2
end(x)
[1] 1961    3
frequency(x)
[1] 4

xts

xts(
  x = NULL,
  order.by = index(x),
  frequency = NULL,
  unique = TRUE,
  tzone = Sys.getenv("TZ"),
  ...
)

x: An object containing the underlying data order.by: A corresponding vector of dates/times of a known time-based

example

library(xts)
xts(1:10, order.by = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10))
           [,1]
2000-01-01    1
2000-04-01    2
2000-07-01    3
2000-10-01    4
2001-01-01    5
2001-04-01    6
2001-07-01    7
2001-10-01    8
2002-01-01    9
2002-04-01   10
x <- xts(1:10, order.by = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10))

str(x)
An xts object on 2000-01-01 / 2002-04-01 containing: 
  Data:    integer [10, 1]
  Index:   Date [10] (TZ: "UTC")
is.xts(x)
[1] TRUE
index(x)
 [1] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01"
 [6] "2001-04-01" "2001-07-01" "2001-10-01" "2002-01-01" "2002-04-01"
coredata(x)
      [,1]
 [1,]    1
 [2,]    2
 [3,]    3
 [4,]    4
 [5,]    5
 [6,]    6
 [7,]    7
 [8,]    8
 [9,]    9
[10,]   10

zoo

zoo(x = NULL, order.by = index(x), frequency = NULL,
  calendar = getOption("zoo.calendar", TRUE))

x: a numeric vector, matrix or a factor
order.by: an index vector with unique entries by which the observations in x are ordered

example

library(zoo)
zoo(1:10, order.by = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10))
2000-01-01 2000-04-01 2000-07-01 2000-10-01 2001-01-01 2001-04-01 2001-07-01 
         1          2          3          4          5          6          7 
2001-10-01 2002-01-01 2002-04-01 
         8          9         10 
x <- zoo(1:10, order.by = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10))

str(x)
'zoo' series from 2000-01-01 to 2002-04-01
  Data: int [1:10] 1 2 3 4 5 6 7 8 9 10
  Index:  Date[1:10], format: "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01" ...
is.zoo(x)
[1] TRUE
index(x)
 [1] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01"
 [6] "2001-04-01" "2001-07-01" "2001-10-01" "2002-01-01" "2002-04-01"
coredata(x)
 [1]  1  2  3  4  5  6  7  8  9 10

timeSeries

timeSeries(data, charvec, units = NULL, format = NULL, zone = "", 
           FinCenter = "", recordIDs = data.frame(), title = NULL, 
           documentation = NULL, ...)

data: a matrix object or any objects which can be coerced to a matrix
charvec: a character vector of dates and times or any objects which can be coerced to a “timeDate” object

Example

library(timeSeries)

timeSeries(1:10, charvec = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10)) 
GMT
           TS.1
2000-01-01    1
2000-04-01    2
2000-07-01    3
2000-10-01    4
2001-01-01    5
2001-04-01    6
2001-07-01    7
2001-10-01    8
2002-01-01    9
2002-04-01   10
as.character(seq(as.Date("2000/1/1"), by = "quarter", length.out = 10))
 [1] "2000-01-01" "2000-04-01" "2000-07-01" "2000-10-01" "2001-01-01"
 [6] "2001-04-01" "2001-07-01" "2001-10-01" "2002-01-01" "2002-04-01"
timeSeries(1:10, charvec = as.character(seq(as.Date("2000/1/1"), by = "quarter", length.out = 10))) 
GMT
           TS.1
2000-01-01    1
2000-04-01    2
2000-07-01    3
2000-10-01    4
2001-01-01    5
2001-04-01    6
2001-07-01    7
2001-10-01    8
2002-01-01    9
2002-04-01   10
x <- timeSeries(1:10, charvec = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10)) 

is.timeSeries(x)
[1] TRUE
start(x)
GMT
[1] [2000-01-01]
end(x)
GMT
[1] [2002-04-01]
time(x)
GMT
 [1] [2000-01-01] [2000-04-01] [2000-07-01] [2000-10-01] [2001-01-01]
 [6] [2001-04-01] [2001-07-01] [2001-10-01] [2002-01-01] [2002-04-01]
as.vector(x)
 [1]  1  2  3  4  5  6  7  8  9 10

tsibble

tsibble(..., key = NULL, index, regular = TRUE, .drop = TRUE)

index: A variable to specify the time index variable.

Example

library(tsibble)
library(dplyr)

x <- tsibble(
  Date = seq(as.Date("2000/1/1"), by = "quarter", length.out = 10),
  Observation = 1:10,
  index = Date
)

x
# A tsibble: 10 x 2 [1D]
   Date       Observation
   <date>           <int>
 1 2000-01-01           1
 2 2000-04-01           2
 3 2000-07-01           3
 4 2000-10-01           4
 5 2001-01-01           5
 6 2001-04-01           6
 7 2001-07-01           7
 8 2001-10-01           8
 9 2002-01-01           9
10 2002-04-01          10
is_tsibble(x)
[1] TRUE
index(x)
Date
measured_vars(x)
[1] "Observation"
x[measured_vars(x)] 
# A tibble: 10 × 1
   Observation
         <int>
 1           1
 2           2
 3           3
 4           4
 5           5
 6           6
 7           7
 8           8
 9           9
10          10
x |> 
  mutate(Month=months(Date))
# A tsibble: 10 x 3 [1D]
   Date       Observation Month  
   <date>           <int> <chr>  
 1 2000-01-01           1 January
 2 2000-04-01           2 April  
 3 2000-07-01           3 July   
 4 2000-10-01           4 October
 5 2001-01-01           5 January
 6 2001-04-01           6 April  
 7 2001-07-01           7 July   
 8 2001-10-01           8 October
 9 2002-01-01           9 January
10 2002-04-01          10 April  

Some Useful Packages

**For Our Course Please Read Section 2.1 of Forecasting: Principles and Practice (3rd ed)