Chapter 10 Dates and times

ds4psy: (10) Dates and times/Time data

This chapter contains essential commands for creating and manipulating dates and times in R. Computing with dates and times is complicated, as it needs to take into account many challenges of culture, history, and science. Seemingly simple questions — like “What time is it?”, “How old is \(X\)?”, and “How long ago did some event \(Y\) take place?” — get surprisingly complicated when we become aware of the relativity of time (in terms of location, let alone physics) and the large variety of units and standards involved in measuring and reporting time.

Dates and times: Working with date and time data in **base** R and **lubridate**.

Figure 10.1: Dates and times: Working with date and time data in base R and lubridate.

An illustration of the pragmatic challenges posed by references to dates and times is provided by the suggestion: Let’s meet tomorrow at 12 PM. Although such a sentence seems common and harmless enough, most people struggle when they begin to think about it. First, the meaning of tomorrow only gains its referent in relation to what we consider to be today. But even if tomorrow’s date was clear, does the appointment time refer to noon (12:00), to early morning (00:00), or to midnight (24:00)?56

If even such simple sentences can seem ambiguous, incorporating a variety of conventions, languages, as well as cultural idiosyncracies (e.g., different notions, expressions and measurements of time) are bound to create confusion. Thus, the key challenge in dealing with dates and times consist in the cultural, historical, and local conventions that need to be taken into account.

Handling all this messy human stuff requires a good deal of conceptual clarification (e.g., to distinguish between time points and different types of time spans), lots of standardization (e.g., of time shifts and time zones), and technical sophistication (e.g., appropriate data types and tools).

After its introduction (Section 10.1), this chapter covers the basics of tackling date- and time-related tasks in R. Its key contents include:

  • Creating, printing, and computing with dates and times in base R (see Section 10.2);

  • Key concepts and commands of the lubridate package (Spinu, Grolemund, & Wickham, 2023) for working with dates and times (see Section 10.3);

  • Applications of these tools to common time-related tasks, as well as simple ds4psy functions for querying dates and times (see Section 10.4).

In the interest of full disclosure: This chapter deals with data that represents calendar dates and local times, but not with issues of timing (i.e., measuring time). R provides tools for recording the time of events and measuring the duration of processes, but the result of such quantifications are usually represented in numeric form.

As it will turn out, learning more about date- and time-related data involves reflecting on the nature of representations, the benefits and costs of standardization, and the importance of tools for dealing with surprises and violations. Ultimately, we will see that studying dates and times can teach us a lot about the world we live in. But before we get carried away, it is time to get started…

References

Spinu, V., Grolemund, G., & Wickham, H. (2023). lubridate: Make dealing with dates a little easier. Retrieved from https://lubridate.tidyverse.org

  1. Pragmatic considerations will probably suggest noon, unless we know each other well enough to meet in the middle of the night.↩︎