1. Converting between zoo and data frame
Great job working with the indices of zoo objects!
2. Converting between data frame and zoo objects
In this lesson, we'll cover how to move between data frames and zoo objects, allowing us to have more comprehensive data science workflows. Data frames are one of the most widely-used classes of R objects, so it's important to be able to work with them when performing time series analysis!
3. data.frame versus zoo
Before we dive into it, let's discuss some of the differences between data frames and zoo objects; specifically, how they differ when they're used with time series data.
Both zoo and data frames can store multiple variables; for example, the temperature, precipitation, and atmospheric pressure each day. In a data frame, these would be stored as separate data frame columns, but in a time series, each "core data" variable is stored as a column of a matrix!
In a data frame, the index is stored just like any other data frame column, while in a zoo object, it's stored as an attribute called "index".
While both zoo and data frames are compatible with the geom functions from ggplot2, we can't plot data frames with the autoplot function.
There's more differences than these; data frames and zoo objects are designed for different purposes, after all, and we'd be comparing apples to oranges!
4. Data frame to zoo
Suppose we have a data frame, called prices, with the columns date and value.
As a reminder, the columns of a data frame can be extracted as vectors – a list of items that share a type, like numeric, Date, or character – with the dollar-sign operator.
Creating a zoo object based on a data frame is very similar to creating one based on vectors; all that's required is to pass the data frame columns to the necessary arguments in the zoo function. We set the x argument, the core data of the time series, to the value column, and order-dot-by, the index, to the date column, and there we have it!
5. Fortifying a zoo object
If we wanted to do the reverse — create a data frame from a zoo object — a good starting point is to take the results of the index and coredata functions, and assign them to data frame columns.
This would work, but theres a better way. The helper function fortify-dot-zoo from the zoo package is meant for this purpose! To use it, we pass our zoo object, and that's it! The output is a data frame, with a column called Index, and additional columns based on the core data of the time series.
6. Let's practice!
Alright, time to practice moving between data frames and zoo objects!