Get startedGet started for free

Times & Dates

1. Times & Dates

One more thing I want to talk to you about are dates and times in R. Time information can come in pretty handy in different situations. For example, imagine you're writing a script that has to be run hourly on a remote server. Generating log files that contain timing information can help you structure these log files and trace potential problems. For other very specific applications such as time-series analyses and seasonality studies, R's power to deal with times and dates will prove extremely useful.

2. Today, right now!

The first step in our exploration will be to simply ask R what the current date is. The output tells us that we're in the year 2015, the fifth month and the seventh day. That is, the seventh of May, 2015. Time is going fast, as always... Is the variable today simply a character string or is there something else going on? Let's have a look to the class() function to see type of the today variable. today is a "Date" object, a special kind of R object that represents dates. To get the current time in R, we use Sys (dot) time(). We get both the time and the date in a very clear format. Notice the difference in capitalization here. Sys (dot) Date() is written with a capital D while Sys (dot) time() is written with a small t. As you'd expect, also the variable now is not a simple string. Let's find out with the class() function. The important class here is POSIXct. Apart from providing rich functionality for calculus and formatting, this class makes sure that the dates and times in R are compatible accross different operating systems, according to the POSIX standard.

3. Create Date objects

Great, we know how to get the current date and time. What about creating Dates for other days? For example, let's say we want to get a Date object for May 14 in 1971. We can use the as (dot) Date() function to convert a character string to a Date. my_date is now an object of class Date. How did R know which elements of the string correspond to the day of the month, which to the month and which to the year? Let's see what happens if we scramble the order of the date elements.

4. Create Date objects

R fails. That's because as (dot) Date() tries some default data formats. It first tries the ISO date format, which is the most common around the globe. This format represents the year with four digits, dash, month as a 2 digit number, dash, day as a two digit number. Because our first character string had this format, R was able to convert the string to a Date. However, for the second example, R failed because the date couldn't be inferred. We can fix this by setting the format argument of as (dot) Date() explicitly. If we specify that the first part of our date is the Year, the second part is the day of the month and the third part is the month, we can have R understand what we mean. If we now print my_date, we see the same date as before, great! There are many more ways to format a date, for example using the full name of a month or a two digit year. You can find a comprehensive list in the interactive exercises.

5. Create POSIXct objects

To convert a string denoting an exact time, we can use the function as (dot) POSIXct(). Once again you could use the format argument inside as (dot) POSIXct to convert character strings with a different layout.

6. Date arithmetic

The cool thing about R objects from the Date and POSIXct classes is that you can do calculations with them. Consider my_date, which we created from a character string earlier. If we simply increment my_date with 1, R will increment the date by 1 day, giving us the 15th of May in 1971. Say we now want to know the difference between the 29th of September in 1998 and my_date. We first create a new data object, my_date2, from a character string. Next, we can simply do a subtraction, calculating the difference in days. There appears to be a time difference of exactly 10000 days, what a coincidence!

7. POSIXct arithmetic

Computations with POSIXct objects happen in the exact same fashion. The only difference is that the "time unit" of POSIXct is not a day as for Date objects, but is a second. Suppose we increment the my_time object we created earlier by 1. We get the following time, so we're one second further in time now. To get the difference between times, let's create a new time, and calculate the difference with my_time. Because the time difference is so large, R simply displays the time difference in days. For any time difference, R will automatically display an easily interpretable time difference.

8. Under the hood

How is R able to do all these calculations so seamlessly? Well, under the hood, R represents dates and times as simple numerics. A Date object is simply a more advanced representation of the number of days since the first of January in 1970. If we unclass the my_date object, thus converting it from a Date to a numeric, we see the number 498, because the 14th of May in 1971 is exactly 498 days from the first of January in 1970. If you're calculating the difference between two days, in fact you're simply calculating the difference between numerical values, and that's something R is particularly good at. The same holds for the POSIXct objects, which are actually simple numerics that hold the number of seconds since the first of January in 1970 at midnight.

9. Dedicated R Packages

Now you know how R handles times and dates behind the scenes, you should have a clear idea of what you can and can't do with dates in R. Now R wouldn't be R if there weren't dedicated packages to deal with times in a more advanced fashion. If you want to learn more, You can check out the lubridate, zoo and xts packages, they're really neat. Time to head over to the exercises now!

10. Let's practice!