Get startedGet started for free

Manipulating zoo objects

1. Manipulating zoo objects

We've discussed creating a zoo time series; let's dive into some functions for manipulating and retrieving time series data.

2. Unordered observations

In the previous lesson, we learned that time series objects created with the zoo package have two defining attributes: the index, representing the time of observation, and the "raw data" stored in the time series. The index gives us the order of observations in our data; using an index is more robust than relying on the default order of vector elements. If our data comes from multiple sources, our observations might not be in the proper order. Using indices ensures that when creating a time series, the value of each observation is mapped correctly to its time.

3. Unordered observations

Creating a zoo object from pairs of unsorted values and times will result in a time series that is automatically sorted, assuming each value matches its index initially. Note that the dates within "ordered_zoo" aren't evenly-spaced; there are large, uneven gaps between each observation! As mentioned earlier in the chapter, zoo is perfectly capable of working with irregular time series.

4. Index

What if we had a zoo object already, and want to only retrieve the index? zoo has a helper function for exactly this: index. To use it, we pass our time series to index, and it returns the "index" attribute – the vector of dates. The index function is also used to update the index of a zoo object. For this, the call to index my_zoo must be on the left-hand side of the assignment operator, the left-facing arrow, and the new index must be to the right.

5. Core data

The second attribute is known as the core data. To retrieve the core data, we can use the coredata function from zoo. The syntax is the same as that of index; we pass our time series as the first argument, and coredata returns the values of our observations, minus the index attribute. Likewise, we can update the values of our time series with the replacement syntax, like what we used for index. For example, let's replace the first element of the core data with a value of thirty, like so. Later in the chapter, we'll cover more about getting the most out of the index and coredata functions!

6. Overlapping time series

When we're dealing with more than one time series, or if our time series data comes from several sources, it's possible to have values that occur in more than one dataset. This can be especially frustrating when combining our time series, we'll see why in a second! There's an asterisk next to the elements that overlap between the time series.

7. Overlapping time series

We get an error when we try to combine these – let's find which indices overlap. We can test our indices using the value matching operator, percent-in-percent. This operator works by testing if any values in the left-hand side match any values in the right-hand side. It returns TRUE or FALSE for each index in first_zoo that matches an index in second_zoo. The next step is to negate our logical statement – we want TRUE for the indices that are NOT matched.

8. Overlapping time series

We now have a logical vector that we can use to subset our time series! Let's assign it to a variable, "subset". If we subset our first_zoo, we can now correctly combine our two time series!

9. Let's practice!

Alright, let's practice working with zoo objects!