4.1 Dealing with Dates

Real world data are often associated with dates and time; however, dealing with dates accurately can appear to be a complicated task due to the variety in formats and accounting for time-zone differences and leap years. R has a range of functions that allow you to work with dates and times. Furthermore, packages such as lubridate make it easier to work with dates and times. This section introduces the basics of dealing with dates.

4.1.1 Getting current date & time

To get current date and time information:

If using the lubridate package:

4.1.2 Converting strings to dates

When date and time data are imported into R they will often default to a character string. This requires us to convert strings to dates. We may also have multiple strings that we want to merge to create a date variable.

4.1.3 Convert Strings to Dates

To convert a string that is already in a date format (YYYY-MM-DD) into a date object use as.Date():

Note that the default date format is YYYY-MM-DD; therefore, if your string is of different format you must incorporate the format argument. There are multiple formats that dates can be in; for a complete list of formatting code options in R type ?strftime in your console.

If using the lubridate package:

One of the many benefits of the lubridate package is that it automatically recognizes the common separators used when recording dates (“-”, “/”, “.”, and ""). As a result, you only need to focus on specifying the order of the date elements to determine the parsing function applied:

Table 4.1: Lubridate Parsing Functions (adapted from Dates and Times Made Easy with Lubridate, Grolemund & Wickham 2011
Order of elements in date-time Parse function
year, month, day ymd()
year, day, month ydm()
month, day, year mdy()
day, month, year dmy()
hour, minute hm()
hour, minute, second hms()
year, month, day, hour, minute, second ymd_hms()

4.1.4 Create Dates by Merging Data

Sometimes your date data are collected in separate elements. To convert these separate data into one date object incorporate the ISOdate() function:

Note that ISODate() also has arguments to accept data for hours, minutes, seconds, and time-zone if you need to merge all these separate components.

4.1.5 Extract & manipulate parts of dates

To extract and manipulate individual elements of a date I typically use the lubridate package due to its simplistic function syntax. The functions provided by lubridate to perform extraction and manipulation of dates include:

Table 4.2: lubridate Accessor Functions (adapted from Dates and Times Made Easy with Lubridate, Grolemund & Wickham 2011
Order of elements in date-time Parse function
year, month, day ymd()
year, day, month ydm()
month, day, year mdy()
day, month, year dmy()
hour, minute hm()
hour, minute, second hms()
year, month, day, hour, minute, second ymd_hms()

To extract an individual element of the date variable you simply use the accessor function desired. Note that the accessor variables have additional arguments that can be used to show the name of the date element in full or abbreviated form.

To manipulate or change the values of date elements we simply use the accessor function to extract the element of choice and then use the assignment function to assign a new value.


4.1.6 Creating date sequences

To create a sequence of dates we can leverage the seq() function. As with numeric vectors, you have to specify at least three of the four arguments (from, to, by, and length.out).

Using the lubridate package is very similar. The only difference is lubridate changes the way you specify the first two arguments in the seq() function.

Creating sequences with time is very similar; however, we need to make sure our date object is POSIXct rather than just a Date object (as produced by as.Date):


4.1.7 Calculations with dates

Since R stores date and time objects as numbers, this allows you to perform various calculations such as logical comparisons, addition, subtraction, and working with durations.

The nice thing about the date/time classes is that they keep track of leap years, leap seconds, daylight savings, and time zones. Use OlsonNames() for a full list of acceptable time zone specifications.

Similarly, the same functionality exists with the lubridate package with the only difference being the accessor function(s) used.

We can also deal with time spans by using the duration functions in lubridate. Durations simply measure the time span between start and end dates. Using base R date functions for duration calculations is tedious and often results in wrong measurements. lubridate provides simplistic syntax to calculate durations with the desired measurement (seconds, minutes, hours, etc.).


4.1.9 Additional resources

For additional resources on learning and dealing with dates I recommend the following: