Get Started

Monthly and quarterly data

1. Monthly and quarterly data

Great job extracting observations from a time series!

2. Dates and aggregated data

In this lesson, we'll cover some of the ways of handling data that occurs monthly or quarterly. Let's look at an example of a Date object in R. Recall that Date and POSIXct objects use the ISO 8601 format, in which the time components are arranged from largest to smallest: year, month, day, and so on. In time series analysis, we often look at data that is aggregated at certain levels; the monthly average of the value, or the highest value each week, etc. Doing so allows us to view the 'bigger picture' of a time series, such as the overall trend in the data, or if the data shows similar values at certain times of year. We'll discuss aggregation later, but consider it a way of 'reducing' our data from a greater sampling frequency to a lower one. Data that represents an entire month might not have a particular date associated with it. If we were to assign a single date to monthly data, which date would we use? Should we use, for example, the first of the month? What about the last day of the month, or the midpoint? Why not ignore the day component entirely?

3. Year-month

In R, there are several ways of 'ignoring' the day of the month and only focusing on the year and month components. We can use the month function from lubridate to extract the numerical month of a particular date, but there's an issue! If we apply this function to dates of differing years, we return the same month, here it's nine for September – we lose important information about the year! Fortunately, there's a solution to this problem, thanks to the zoo package. zoo comes with a class of objects called yearmon, which stands for year-month. It combines the year and the month information together, meaning we can ignore the day component without losing any of the other information!

4. zoo::yearmon

To convert to year-month data, we can use the as-dot-yearmon function from the zoo package. This function can handle a variety of inputs, like a character vector, formatted in ISO 8601, an object with the Date class, a POSIXct object, or a numeric, decimal date object!

5. Monthly data

How about an example of yearmon in action? Let's look at the time series monthly_sales – a series based on the monthly total of sales at a particular company, in hundreds of thousands of US Dollars. Printing the time series to the console reveals that the index is of class "yearmon". The temporal information combines the year and month components, but ignores the day of the month.

6. zoo::yearqtr

Likewise, there is also a data class from the zoo package that can interpret and manipulate quarterly data. Quarterly data divides the year into quarters, and is most often seen in marketing and business contexts. To find the year-quarter, we use the as-dot-yearqtr function from zoo, like so. This function works similar to as-dot-yearmon; the input vector can be numeric, Date, and even the yearmon class! Unfortunately, though, this function doesn't work with character data; you have to convert to another class, like Date, first.

7. Quarterly data

Here's an example of quarterly data; our monthly_sales time series from earlier. It's now been aggregated to the yearqtr class and saved as quarterly_sales; we'll cover how to perform aggregation in the next lesson!

8. Let's practice!

Alright, let's go to the exercises and practice working with monthly and quarterly data!