Day3 Learning Notes : Dates and Times

 

R has developed a special representation for dates and times. Dates are represented by the Date class and times are represented by the POSIXct or the POSIXlt class. Dates are stored internally as the number of days since 1970-01-01 while times are stored internally as the number of seconds since 1970-01-01.

It’s not important to know the internal representation of dates and times in order to use them in R. I just thought those were fun facts.


Dates in R

Dates are represented by the Date class and can be coerced from a character string using the as.Date() function. This is a common way to end up with a Date object in R.

You can see the internal representation of a Date object by using the unclass() function.

#Comment offset day since 1970-01-01

 


Times in R

Times are represented by the POSIXct or the POSIXlt class. POSIXct is just a very large integer under the hood. It use a useful class when you want to store times in something like a data frame. POSIXlt is a list underneath and it stores a bunch of other useful information like the day of the week, day of the year, month, day of the month. This is useful when you need that kind of information.

There are a number of generic functions that work on dates and times to help you extract pieces of dates and/or times.

  • weekdays: give the day of the week
  • months: give the month name
  • quarters: give the quarter number (“Q1”, “Q2”, “Q3”, or “Q4”)

Times can be coerced from a character string using the as.POSIXlt or as.POSIXct function.

The POSIXlt object contains some useful metadata.

You can also use the POSIXct format.

Finally, there is the strptime() function in case your dates are written in a different format.

strptime() takes a character vector that has dates and times and converts them into to a POSIXlt
object.

The weird-looking symbols that start with the % symbol are the formatting strings for dates and times. I can never remember the formatting strings. Check ?strptime for details. It’s probably not worth memorizing this stuff.

 


Operations on Dates and Times

You can use mathematical operations on dates and times. Well, really just + and -. You can do comparisons too (i.e. ==, <=)

The nice thing about the date/time classes is that they keep track of all the annoying things about dates and times, like leap years, leap seconds, daylight savings, and time zones.

Here’s an example where a leap year gets involved.

Here’s an example where two different time zones are in play (unless you live in GMT timezone, in which case they will be the same!).

 


Summary

  • Dates and times have special classes in R that allow for numerical and statistical calculations
  • Dates use the Date class
  • Times use the POSIXct and POSIXlt class
  • Character strings can be coerced to Date/Time classes using the strptime function or the as.Date, as.POSIXlt, or as.POSIXct

Copied from R Programming for Data Science – Roger D. Peng

Leave a Reply

Your email address will not be published. Required fields are marked *