Reader small image

You're reading from  Hands-On Time Series Analysis with R

Product typeBook
Published inMay 2019
Reading LevelBeginner
PublisherPackt
ISBN-139781788629157
Edition1st Edition
Languages
Right arrow
Author (1)
Rami Krispin
Rami Krispin
author image
Rami Krispin

Rami Krispin is a data scientist at a major Silicon Valley company, where he focuses on time series analysis and forecasting. In his free time, he also develops open source tools and is the author of several R packages, including the TSstudio package for time series analysis and forecasting applications. Rami holds an MA in Applied Economics and an MS in actuarial mathematics from the University of MichiganAnn Arbor.
Read more about Rami Krispin

Right arrow

Working with Date and Time Objects

The main attribute of time series data is its timestamp, which could be a date object, time object, or other index format depending on the series frequency. Typically while loading raw data, it is not trivial to have the date or time object formatted and ready to use. Therefore, it is most likely that the raw data may require some reformatting before you are able to transform your data into time series format. The ability to work with time and date objects is an essential part of the data preparation process. In this chapter, we will introduce a set of tools and applications for dealing with those objects, starting with R's built-in tools and classes from the base package and moving to the advanced applications of the lubridate package.

In this chapter, we will cover the following topics:

  • The date and time formats
  • Date and time objects...

Technical requirements

The date and time formats

One of the main challenges of working with date and time objects is the variety of formats that can be used for representing date and time. For example, most of the common calendar systems use an alphabetical form to represent the three date components:

  • Y: Refers to the year, which can display either using the yy (two-digits year, for example, 18) or yyyy (four-digit year, for example, 2018) formats.
  • M: Refers to the month. Here there are four methods to display the month:
    • m: One-digit month (the first 9 months represented by a single digit, for example, 1 for January, 2 for February, and so on)
    • mm: Two-digit month (the first 9 months represented by two digits, for example, 01 for January, 02 for February, and so on)
    • mmm: Three-letter abbreviation for a month (for example, Jan for January, Feb for February, and so on)
    • mmmm: Full month name (for example...

Date and time objects in R

The base package, one of R's core packages, provides two types of date and time classes:

  1. Date: This is a simple representation of a calendar date following the ISO 8601 international standard format (or the Gregorian calendar format) using the YYYY-m-d date format. Each date object has a numeric value of the number of days since the origin point (the default setting is 1970-01-01). In the Handling numeric date objects section in this chapter, we will discuss the usage of the origin in the reformatting process of date objects in more detail. It will make sense to use this format when the frequency of the data is daily or lower (for example, monthly, quarterly, and so on) and the time of the day doesn't matter.
  2. POSIXct/POSIXlt: Also known as the DateTime classes (that is, they represent both date and time), these are two POSIX date/time classes...

Creating a date or time index

So far, our focus in this chapter was mainly on the attributes of the date and time classes. Let's now connect the dots and see some useful applications of time series data. As introduced in Chapter 1, Introduction to Time Series Analysis and R, the main characteristic of time series data is its time index (or timestamp), an equally spaced time interval. The base package provides two pairs of functions, seq.Date and seq.POSIXt, to create a time index vector with Date or POSIX objects respectively. The main difference between the two functions (besides the class of the output) is the units of the time interval. It will make sense to use the seq.Date function to generate a time sequence with daily frequency or lower (for example, weekly, monthly, and so on) and as.POSIXt in other instances (for higher frequencies than daily, such as hourly, half...

Manipulation of date and time with the lubridate package

The title of the lubridate package documentation in CRAN is Make Dealing with Dates a Little Easier. In my mind, this is a very modest title for a package that makes work with date and time objects more effective, simple, and time efficient. This section introduces alternative tools and applications with the lubridate package for reformatting, converting, and handling date and time objects.

Reformatting date and time objects – the lubridate way

To understand how simple it is to reformat date and time objects with the lubridate package, let's go back to the complex time object (Monday, December 31, 2018 11:59:59 PM) we converted earlier to a POSIXct class...

Summary

Date and time objects are one of the foundations of the time series data. Thus, the ability to import, reformat, and convert this type of object in R seamlessly is an essential part of the time series analysis process. In this chapter, we introduced the primary date and time objects in R, the Date and POSIXct/POSIXlt classes, and their main attributes. Furthermore, we introduced two main approaches in R to handle and process those objects, with the base and lubridate packages. While the work with the base functions is more technical (or hardcore coding), the work with the lubridate package is based on common English language communication with the objects and therefore is much simpler to use. I personally found that deep understanding of the base package approach makes working with the lubridate package much smoother and more straightforward, as date and time objects play...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Time Series Analysis with R
Published in: May 2019Publisher: PacktISBN-13: 9781788629157
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Rami Krispin

Rami Krispin is a data scientist at a major Silicon Valley company, where he focuses on time series analysis and forecasting. In his free time, he also develops open source tools and is the author of several R packages, including the TSstudio package for time series analysis and forecasting applications. Rami holds an MA in Applied Economics and an MS in actuarial mathematics from the University of MichiganAnn Arbor.
Read more about Rami Krispin