How to Add Time Information to Dates in R

Often, referring only to dates in R isn’t enough. There are times when you also need to indicate a specific time in hours and minutes in R.

To specify time information in addition to dates, you can choose between two functions in R: as.POSIXct() and as.POSIXlt(). These two datetime functions differ in the way that they store date information internally, as well as in the way that you can extract date and time elements. (For more on these two functions, see the nearby sidebar, “The two datetime functions.”)

POSIX is the name of a set of standards that refers to the UNIX operating system. POSIXct refers to a time that is internally stored as the number of seconds since the start of 1970, by default. (You can modify the origin year by setting the origin argument to POSIXct().) POSIXlt refers to a date stored as a names list of vectors for the year, month, day, hours, and minutes.

According to Wikipedia, the time of the Apollo 11 moon landing was July 20, 1969, at 20:17:39 UTC. (UTC is the acronym for Coordinated Universal Time. It’s how the world’s clocks are regulated.) To express this date and time in R, try the following:

> apollo <- "July 20, 1969, 20:17:39"
> apollo.fmt <- "%B %d, %Y, %H:%M:%S"
> xct <- as.POSIXct(apollo, format=apollo.fmt, tz="UTC")
> xct
[1] "1969-07-20 20:17:39 UTC"

As you can see, as.POSIXct() takes similar arguments to as.Date(), but you need to specify the date format as well as the time zone.

Format Description
%H Hours as a decimal number (00–23)
%I Hours as a decimal number (01–12)
%M Minutes as a decimal number (00–59)
%S Seconds as a decimal number (00–61)
%p AM/PM indicator

In most computer languages and systems, dates are represented by numeric values that indicate the number of seconds since a specific instant in time (known as the epoch).

In R, you can use two functions to work with datetime objects: POSIXct() and POSIXlt(). These functions create objects of class POSIXct and POSIXlt, respectively:

  • POSIXct objects represents the (signed) number of seconds since the beginning of 1970 (in the UTC time zone) as a numeric vector.

  • POSIXlt objects are named lists of vectors representing nine elements of a datetime (sec, min, hour, and so on).

Because POSIXct are numbers, and POSIXlt objects are lists, POSIXct objects requires less memory.

This summarizes the main differences between the different datetime classes in R.

Class Description Useful Functions
Date Calendar date as.Date()
POSIXct The number of seconds since the beginning of 1970 (in the UTC time zone) as a numeric vector as.POSIXct()
POSIXlt A named list of vectors representing nine elements (sec, min, hour, and so on) as.POSIXlt()
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com