Get startedGet started for free

Making irregular data regular

1. Making irregular data regular

Welcome to chapter 4! Previous chapters taught you various tools to help you import data into your R session. Most of the examples used data that is usually referred to as "regular", meaning that the observations occur at equally-spaced points in time.

2. Regular date-time sequences

Creating a regular dataset from the raw irregular data can make it easier to merge with other series, making analysis easier. The first thing you need to create regular dataset is a regular sequence of date-times that span the irregular dataset. The seq() function makes it easy to create regular date-time sequences. If your xts object contains a Date index, seq-dot-Date() will be used, while seq-dot-POSIXt() will be used if your xts object contains a POSIXct index. The "by" argument specifies how often to create an observation in the sequence.

3. start() and end()functions

The xts package provides start() and end() functions that extract the first and last index values from an xts object. You can use the start() and end() functions to set the values for the "from" and "to" arguments to seq(). Again, this same code will work whether you have daily or intraday data.

4. Zero-width xts objects

Once you have a regular sequence of date-times, you can create a zero-width xts object. A zero-width xts object has an index, but no columns of data. Zero-width xts objects are useful when you want to add or remove rows from another xts object, without adding a column.

5. Creating regular from irregular data

When creating regular data from irregular data, you usually need to add observations at each date-time in the regular sequence, because it's very unlikely for irregular data to occur at exactly a regular date-time. These added observations will be NA in the result, because they don't occur in the irregular data, and the zero-width xts object has no data column.

6. Merge irregular xts with regular zero-width xts

Let's look at example, where you have some irregular daily price data. You can see there are observations for the 2nd, 4th, and 10th days, but no observations for any other days. You can use start(), end(), and seq() to create a regular daily sequence. Then you can create a zero-width xts object using that sequence.

7. Merge irregular xts with regular zero-width xts

After you merge the zero-width xts object with the original data, you can see the missing regular observations that are added to the result.

8. Filling missing values

Depending on your analysis, you may want to replace these missing values. One common solution is to use the last observation carried forward, or L-O-C-F. You can do this by using the na-dot-locf() function on the result of the merge() call.

9. Filling missing values

Or you can set the merge() function's fill argument to na-dot-locf.

10. Let's practice!

Now that you've learned some tools to make regular data from irregular data, it's time to practice using those tools in the exercises.