R tutorial: xts & zoo for time series analysis

Learn how xts objects store time series data as a matrix paired with a time index in R. The index must be a true time class like Date or POSIXct, not a string, and xts automatically sorts unsorted data. Use coredata to extract the matrix and index to pull out the time vector.
Chapters
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 an XTS is a zoo, 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, time, date, cron, and more. But they need to be time Based on here we're using R's date objects. this point, though we don't have a time series. We'll need to join these to create our XDS object. To do this, we call the XTS constructor with our data x and pass our dates IDX to orderby. The constructor has a few optional arguments, the most useful being tzone to set time zones and unique, which will force all times to 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 an 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 that 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 row names, but remember, it's 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 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 of the power of zoo methods for free. We'll see how important this is throughout the course. One last point before we break out the exercises. Sometimes it'll be necessary to reverse the steps and 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. Core data 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.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







