R - Date Time (POSIXct)

> Procedural Languages > R

1 - About

Time representation in R with the POSIXct Date-Time Classes.

See also: Time - (Unix|POSIX|Epoch) time

Advertising

3 - Management

3.1 - Generation

  • seq.POSIXt generate Regular Sequences of Times
# A whole year
seq(
        from = as.POSIXct("2017-01-01", tz = "GMT"), 
        length.out = 365, 
        by = "days")
 
?seq.POSIXt

3.2 - Conversion

  • From other classes
as.POSIXct("2016-01-01 00:00:00", tz="UTC")
as.POSIXct("2016-01-01 00:00:00", format="%Y-%m-%d %H:%M:%S", tz="UTC")
as.POSIXct("17:23:54", format="%H:%M:%S", tz="UTC")
  • For conversion to and from character representations.
?strptime 
# Week day
format(Sys.time(),format="%u")
  • from Numeric Part
ISOdate(2001, 1, 1, tz = "")
ISOdate(2001, 1, 1, 7, 30, 26, tz = "")
  • from seconds
as.POSIXct(secondSince, tz = "UTC", origin="1970-01-01") 

3.3 - Current Time

Sys.time()
# Format
format(Sys.time(), "%Y-%m-%d %H:%M:%S")
# The current time in GMT
as.POSIXlt(Sys.time(), "GMT") 
# Type
str(Sys.time())
 POSIXct[1:1], format: "2017-02-22 15:27:47"
Advertising

3.4 - Add/Subtract time

  • An hour ago
Sys.time() - 3600
  • A diff
# Current time + 30 minutes, 3 times
Sys.time() + as.difftime(30*(0:2),units="mins")
[1] "2017-02-22 15:34:36 CET" "2017-02-22 16:04:36 CET" "2017-02-22 16:34:36 CET"

3.5 - Diff

  • Substraction gives a diffTime
endDate - startDate
  • As minute and ready for Ggplot
as.numeric(difftime(endDate , startDate, units=c("mins"))

3.6 - Date Time Extraction

  • Day (trunc)
trunc(Sys.time(), "day")
[1] "2017-02-23 CET"
  • weekdays return a character vector of names in the locale in use.
weekdays(Sys.time())
#As Factor: factor(weekdays(res$CREATED_ON),levels=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"))
[1] "Thursday"
  • months return a character vector of names in the locale in use.
months(Sys.time())
[1] "February"
  • quarters returns a character vector of “Q1” to “Q4”.
quarters(Sys.time())
[1] "Q1"
  • year
format(Sys.time(), "%Y")
[1] "2017"
Advertising

3.7 - Round

round(Sys.time(), "hour")
[1] "2017-02-23 10:00:00 CET"

3.8 - Comparison

time1 lop time2

where:

  • lop is one of ==, !=, <, , > or >=

3.9 - Time Diff

  • julian returns the number of days (possibly fractional) since the origin, with the origin as a “origin” attribute. All time calculations in R are done ignoring leap-seconds.
julian(Sys.time())
Time difference of 17220.38 days

3.10 - Loop

startDate = as.POSIXct("2016-01-01 10:10:00", tz="UTC");
endDate = as.POSIXct("2016-01-01 10:30:00", tz="UTC")
 
bin = seq(
  from = startDate, 
  to = endDate,
  by = "5 min")
 
# The as.list below is important because the output of seq is not a vector
# the for construct expect a vector
# is.vector(bin)
# [1] FALSE
 
for(i in as.list(bin)){
  print(i)
}
[1] "2016-01-01 10:10:00 UTC"
[1] "2016-01-01 10:15:00 UTC"
[1] "2016-01-01 10:20:00 UTC"
[1] "2016-01-01 10:25:00 UTC"
[1] "2016-01-01 10:30:00 UTC"

3.11 - Binning

Time Binning Convert a Date-Time Object to a Factor is done with the cut.PosixCt function

?cut.POSIXt

A factor is returned. If you want to plot the data, you need to coerce the data again with as.POSIXct

See also: library(scales) # to access breaks/formatting functions

3.11.1 - Basic

# 25 random number between 0 and 1
rand = sample(1:100,25, replace=TRUE)/100
 
# Create a time vector with the random number
x = Sys.time() + as.difftime(5*rand,units="mins")
 
# Cut it in minutes
cut(x, breaks="1 min")
 
# in hour, ...
# cut(x, breaks="1 hour")
[1] 2017-02-22 15:59:00 2017-02-22 15:58:00 2017-02-22 15:55:00 2017-02-22 15:57:00 2017-02-22 15:55:00
 [6] 2017-02-22 15:59:00 2017-02-22 15:55:00 2017-02-22 16:00:00 2017-02-22 15:57:00 2017-02-22 15:57:00
[11] 2017-02-22 15:55:00 2017-02-22 15:56:00 2017-02-22 15:57:00 2017-02-22 15:58:00 2017-02-22 15:58:00
[16] 2017-02-22 15:59:00 2017-02-22 15:57:00 2017-02-22 15:57:00 2017-02-22 15:58:00 2017-02-22 15:58:00
[21] 2017-02-22 15:56:00 2017-02-22 15:57:00 2017-02-22 15:56:00 2017-02-22 15:58:00 2017-02-22 15:56:00
6 Levels: 2017-02-22 15:55:00 2017-02-22 15:56:00 2017-02-22 15:57:00 ... 2017-02-22 16:00:00

3.11.2 - Break Defined

The below example shows how to cut the time part. All the date must have the same constant date part (we use 2017-06-06)

  • Create cut points (Because cutting appears to be a less than, we start at 00:30.
cut_point = seq(as.POSIXct("2017-06-06 00:30:00",tz="UTC"),by="30 min",length.out=48)
  • Use them
cut(x, breaks=cut_point)