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.
Data used in this chapter.
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.
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
.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.
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