What about times?

Working with Dates and Times in R

Charlotte Wickham

Instructor

ISO 8601

HH:MM:SS

  • Largest unit to smallest
  • Fixed digits
    • Hours: 00 -- 24
    • Minutes: 00 -- 59
    • Seconds: 00 -- 60 (60 only for leap seconds)
  • No separator or :
Working with Dates and Times in R

Datetimes in R

  • Two objects types:
    • POSIXlt - list with named components
    • POSIXct - seconds since 1970-01-01 00:00:00
  • POSIXct will go in a data frame
  • as.POSIXct() turns a string into a POSIXct object

      x <- as.POSIXct("1970-01-01 00:01:00")
      str(x)
    
       POSIXct[1:1], format: "1970-01-01 00:01:00"
    
Working with Dates and Times in R

Timezones

  • "2013-02-27T18:00:00" - 6pm local time

  • "2013-02-27T18:00:00Z" - 6pm UTC

  • "2013-02-27T18:00:00-08:00" - 6pm in Oregon

as.POSIXct("2013-02-27T18:00:00Z")
"2013-02-27 PST"
as.POSIXct("2013-02-27T18:00:00Z", tz = "UTC")
"2013-02-27 UTC"
Working with Dates and Times in R

Datetimes behave nicely too

Once a POSIXct object, datetimes can be:

  • Compared

  • Subtracted

  • Plotted

Working with Dates and Times in R

Let's practice!

Working with Dates and Times in R

Preparing Video For Download...