10.1 Introduction

Beyond text, there is another prominent data type that we have not covered so far: Dates and times.

Both are complicated, for several reasons:

  • they depend on units of magnitude and measurement: years, months, weeks, days, for dates, and hours, minutes, seconds for times.
  • their values depend on locations: date and time zones, daylight savings times
  • they depend on cultural contexts (e.g., weeks starting on Sunday vs. Monday) and languages (saturday - Samstag - samedi)
  • they depend on pragmatic considerations — like our current interests and communicative intents: 14 days vs. two weeks vs. bi-monthly

Another complication: We want to do different things with them: Denote points in time (moments) vs. measure durations (as differences between time points) or time intervals (durations with a starting and ending point)

Dealing with all this messy human stuff requires standardization and implies technical sophistication (e.g., for computing and rounding in different units of time).

10.1.1 Objectives

After working through this chapter, you should be able to:

  1. understand the basic units of dates and times, and different time zones,
  2. understand the 2 Date-Time Classes of R,
  3. use base R functions to determine your current time and enter or parse Date objects,
  4. use lubridate functions to enter and compute with dates and times,
  5. use ds4psy functions to query dates and times.

10.1.2 Data

This chapter uses the sample_date() and sample_time() functions from the ds4psy package (as well as a tibble fame that includes some date variables).

10.1.3 Getting ready

This chapter formerly assumed that you have read and worked through Chapter 16: Dates and times of the r4ds book (Wickham & Grolemund, 2017). It now can be read by itself, but reading Chapter 16 of r4ds is still recommended.

Please do the following to get started:

  • Create an R Markdown (.Rmd) document (for instructions, see Appendix E and the templates linked in Section E.2).

  • Structure your document by inserting headings and empty lines between different parts. Here’s an example how your initial file could look:

---
title: "Chapter 10: Time data"
author: "Your name"
date: "2020 May 25"
output: html_document
---

Add text or code chunks here.

# Exercises (10: Time data)
  
## Exercise 1
  
## Exercise 2
  
etc. 
  
<!-- The end (eof). -->
  • Create an initial code chunk below the header of your .Rmd file that loads the R packages of the tidyverse (and see Section E.3.3 if you want to get rid of the messages and warnings of this chunk in your HTML output).

  • Save your file (e.g., as nr_name.Rmd in the R folder of your current project) and remember saving and knitting it regularly as you keep adding content to it.

References

Wickham, H., & Grolemund, G. (2017). R for data science: Import, tidy, transform, visualize, and model data. Retrieved from http://r4ds.had.co.nz