6 Transformations Involving Dates and Times

 

This chapter covers

  • Understanding the common pitfalls of handling data with dates and times
  • Using functions from the lubridate package to effectively work with date and time values
  • Using functions from the hms package to create time values
  • Plotting time series data in ggplot

Working with dates and times (and both together as date-time values) is virtually unavoidable. Many event-based datasets will have a date/time element and there are many other examples of datasets with dates and/or times. Further to this, dates and times are often provided in less-than-desirable forms (not adhering to standards), so, we’ll need to transform these data into more dependable formats. Once we gain some understanding of how dates and times can be used in tabular data, we are better situated to use the data and create data extracts, reports, and ggplot plots.

The edr package provides the dataset nycweather, which will be used throughout this chapter to illustrate the ways we can work with dates and times in R. It is similar in spirit to the winniweather dataset that we used in Chapter 4 but is significantly larger (i.e., it has an entire year of weather data). Be sure to check out the help file for the dataset by using help(nycweather) in the console, it has information on all of the different variables.

6.1          Dates, Date-Times, and ISO 8601

6.2       Using lubridate to Parse Date and Date-Time Strings

6.2.1   Parsing Dates and Getting R Date Values

6.2.2   Parsing Date-Times and Getting POSIXct Values

6.3       Transforming Dates and Times in Tabular Data

6.3.1   Creating a Reasonable Time Series Plot with ggplot

6.3.2   Further Transforming the Time Series Data to Make a New Plot

6.4       Summary