Session Ready
Exercise

Modifying timestamps

Most time series we've seen have been daily or lower frequency. Depending on your field, you might encounter higher frequency data - think intraday trading intervals, or sensor data from medical equipment.

In these situations, there are two functions in xts that are handy to know.

If you find that you have observations with identical timestamps, it might be useful to perturb or remove these times to allow for uniqueness. xts provides the function make.index.unique() for just this purpose. The eps argument, short for epsilon or small change, controls how much identical times should be perturbed, and drop = TRUE lets you just remove duplicate observations entirely.

On other occasions you might find your timestamps a bit too precise. In these instances it might be better to round up to some fixed interval, for example an observation may occur at any point in an hour, but you want to record the latest as of the beginning of the next hour. For this situation, the align.time() command will do what you need, setting the n argument to the number of seconds you'd like to round to.

make.index.unique(x, eps = 1e-4)  # Perturb
make.index.unique(x, drop = TRUE) # Drop duplicates
align.time(x, n = 60) # Round to the minute

In this exercise, you'll try the three use cases on an xts object called z.

Instructions
100 XP
  • Convert the z series times to a unique series using make.index.unique(), where eps = 1e-4. Save this to z_unique.
  • Remove duplicate times in z. Save this to z_dup.
  • Align z to the nearest hour using align.time(). Save this to z_round.