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

Data used in this chapter.

10.1.3 Preparation


This chapter assumes that you have read and worked through Chapter 16: Dates and times of the r4ds textbook (Wickham & Grolemund, 2017). Based on this background, we examine essential commands of base R and the lubridate package (Spinu et al., 2018) in the context of examples and exercises.

Getting ready

  • 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:

  • 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.


Spinu, V., Grolemund, G., & Wickham, H. (2018). lubridate: Make dealing with dates a little easier. Retrieved from https://CRAN.R-project.org/package=lubridate

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