Learn more about time series analysis with xts & zoo: https://www.datacamp.com/courses/manipulating-time-series-data-in-r-with-xts-zoo
So, what is xts? xts stands for "eXtensible time series"; Objects that are designed to be flexible and powerful - designed to make using time series easy.
At the heart of xts is a zoo object, a matrix object plus a vector of times corresponding to each row, which in turn represents an observation in time.
Visually, you can think of this as data plus an array of times.
To illustrate, we'll create a simple matrix called "x". Each row of our data is an observation in time.
To track these observations we have dates in an object called "idx". Note that this index must be a true time object, not a string or number that looks like time. Now, xts lets you use nearly any time class - be it of class Date, POSIX times, timeDate, chron and more - but they need to be time based. Here we are using R's Date objects.
At this point though we don't have a time series. We'll need to join these to create our xts object.
To do this, we call the xts constructor with our data "x" and pass our dates "idx' to order.by.
The constructor has a few optional arguments, the most useful being "tzone" - to set time zones and "unique", which will force all times be unique. Note that xts doesn't enforce uniqueness for your index, but you may require this in your own applications.
One thing to note is that your index should be in increasing order of time. Earlier observations at the top of your object, and later more recent observations toward the bottom. If you pass in a non-sorted vector, xts will reorder your index and the corresponding rows of your data to ensure you have a properly ordered time series.
Looking back to the example, you can see that we now have a matrix of values with dates on the left. They may look like rownames, but remember its really our index.
So what makes xts special?
As I mentioned before - xts is a matrix that has associated times for each observation. Basic operations work just like they would on a matrix, almost. One difference you'll note is that subsets will always preserve the object's 'matrix' form - choose one or more than one column will always results in another matrix object.
Another difference is that attributes are generally preserved as you work with your data - so if you store something like a timestamp of when you acquired the data in an 'xts attribute' subsetting won't cause that information to be lost.
Finally since xts is a subclass of zoo, you get all the power of zoo methods for free. We'll see how important this is throughout the course.
One final point before we break out the exercises. Sometimes it will be necessary to reverse the steps we took to create the time series, and instead extract our raw data or raw times for use in other contexts. xts provides two functions that we'll cover here. coredata() is how you get the raw matrix back, and index() is how you extract the dates or times. Simple and effective.
Now, let's get to work!