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).
After working through this chapter, you should be able to:
- understand the basic units of dates and times, and different time zones,
- understand the 2 Date-Time Classes of R,
- use base R functions to determine your current time and enter or parse
- use lubridate functions to enter and compute with dates and times,
- use ds4psy functions to query dates and times.
This chapter uses the
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:
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
.Rmdfile 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.Rmdin the R folder of your current project) and remember saving and knitting it regularly as you keep adding content to it.
Wickham, H., & Grolemund, G. (2017). R for data science: Import, tidy, transform, visualize, and model data. Retrieved from http://r4ds.had.co.nz