1. Learn
  2. /
  3. Courses
  4. /
  5. Manipulating Time Series Data with xts and zoo in R

Exercise

Time based indices

xts objects get their power from the index attribute that holds the time dimension. One major difference between xts and most other time series objects in R is the ability to use any one of various classes that are used to represent time. Whether POSIXct, Date, or some other class, xts will convert this into an internal form to make subsetting as natural to the user as possible.

a <- xts(x = 1:2, as.Date("2012-01-01") + 0:1)
a[index(a)]

We'll get into more detail on subsetting xts objects in a later chapter. For now you can simply use date objects to index appropriate rows from your time series. You can think of this as effectively matching the rownames you see in the object. This works as anticipated for time objects because the rownames are really dates!

For this exercise you'll create two time series using two different time classes. You will then subset each object using the other object's index.

Instructions

100 XP
  • Create an object of 5 dates called dates starting at "2016-01-01".
  • Create a time series ts_a using the numbers 1 through 5 as your data, and dates as your order.by index.
  • Create a time series ts_b using the numbers 1 through 5 as your data, and the same dates, but as POSIXct objects.
  • Use the index from ts_b to extract the dates from ts_a.
  • Now do the reverse, indexing ts_b using the times from ts_a.