Session Ready
Exercise

Your first xts object

xts objects are simple. Think of them as a matrix of observations combined with an index of corresponding dates and times.

  xts = matrix + times

The main xts constructor takes a number of arguments, but the two most important are x for the data and order.by for the index. x must be a vector or matrix. order.by is a vector which must be the same length or number of rows as x, be a proper time or date object (very important!), and be in increasing order.

xts also allows you to bind arbitrary key-value attributes to your data. This lets you keep metadata about your object inside your object. To add these at creation, you simply pass additional name = value arguments to the xts() function.

Since we are focusing here on the mechanics, we'll use random numbers as our data so we can focus on creating the object rather than worry about its contents.

Instructions
100 XP
  • Create an object called data that contains five random numbers using rnorm().
  • Create a Date class index from "2016-01-01" of length five called dates.
  • Use the xts constructor to create an object called smith using data and dates as the index.
  • Create an object called bday which contains a POSIXct date object containing the date "1899-05-08".
  • Create an xts object called hayek using data, dates, and a new attribute called born, which should contain the birthday object you just created.