Manipulating zoo objects

Manipulating Time Series Data in R

Harrison Brown

Graduate Researcher in Geography

Unordered observations

Key time series attributes:

  • Index
    • Order of observations
    • Allows time series to be sorted and compared
    • Most often defined by date or time
unordered_dates
[1] "1985-02-15" "1977-03-01"
[3] "1970-06-10" "1971-01-16"
[5] "1973-07-28" "1976-11-24"
...
unordered_values
[1] 10.57  9.64  8.47 
[4] 9.07  9.16  9.39
Manipulating Time Series Data in R

Unordered observations

ordered_zoo <-
 zoo(unordered_values,
     order.by = unordered_dates)

head(ordered_zoo)
1970-06-10  8.47
1971-01-16  9.07
1973-07-28  9.16
1976-11-24  9.39
1977-03-01  9.64
1982-10-12 10.04
autoplot(ordered_zoo)

Plot of the "ordered_zoo" time series; the values and dates in the time series are made up, and do not represent any real-world data. The plot follows a general upward trend, and the index ranges from 1970 to 1995.

Manipulating Time Series Data in R

Index

zoo::index()

index(my_zoo)
[1] "2022-01-20" "2022-01-21"
[3] "2022-01-22" "2022-01-23"
[5] "2022-01-24" "2022-01-25"
...
index(my_zoo) <- new_index

head(my_zoo)
2010-01-20 4.224
2010-01-21 5.645
2010-01-22 5.919
2010-01-23 6.191
2010-01-24 6.871
2010-01-25 6.606
Manipulating Time Series Data in R

Core data

zoo::coredata()

coredata(my_zoo)
[1]  4.224 5.645 5.919
[4]  6.191 6.871 6.606
[7]  5.496 6.073 7.054 
[10] 6.849 7.291 7.064
...
coredata(my_zoo)[1] <- 30

head(my_zoo)
2022-01-20 30.000
2022-01-21  5.645
2022-01-22  5.919
2022-01-23  6.191
2022-01-24  6.871
2022-01-25  6.606
Manipulating Time Series Data in R

Overlapping time series

first_zoo
2022-08-01  4.06
2022-08-02 10.45
2022-08-03 15.43 *
2022-08-04 15.17 *
2022-08-05 19.87 *
2022-08-06 20.50 *
second_zoo
2022-08-03 15.43 *
2022-08-04 15.17 *
2022-08-05 19.87 *
2022-08-06 20.50 *
2022-08-07 18.56
2022-08-08 16.37
Manipulating Time Series Data in R

Overlapping time series

c(first_zoo, second_zoo)
Error in rbind.zoo(...) :
    indexes overlap
index(first_zoo) %in% index(second_zoo)
[1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE
!index(first_zoo) %in%
  index(second_zoo)
[1]  TRUE  TRUE FALSE FALSE FALSE FALSE
Manipulating Time Series Data in R

Overlapping time series

subset <- !index(first_zoo) %in% 
            index(second_zoo)
c(first_zoo[subset], second_zoo)
2022-08-01  4.06
2022-08-02 10.45
2022-08-03 15.43
2022-08-04 15.17
2022-08-05 19.87
2022-08-06 20.50
2022-08-07 18.56
2022-08-08 16.37
Manipulating Time Series Data in R

Let's practice!

Manipulating Time Series Data in R

Preparing Video For Download...