Table of Contents

R - Date Time (POSIXct)

About

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

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

Management

Generation

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

Conversion

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")
?strptime 
# Week day
format(Sys.time(),format="%u")
ISOdate(2001, 1, 1, tz = "")
ISOdate(2001, 1, 1, 7, 30, 26, tz = "")
as.POSIXct(secondSince, tz = "UTC", origin="1970-01-01") 

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"

Add/Subtract time

Sys.time() - 3600
# 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"

Diff

endDate - startDate
as.numeric(difftime(endDate , startDate, units=c("mins"))

Date Time Extraction

trunc(Sys.time(), "day")
[1] "2017-02-23 CET"

weekdays(Sys.time())
#As Factor: factor(weekdays(res$CREATED_ON),levels=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"))
[1] "Thursday"

months(Sys.time())
[1] "February"

quarters(Sys.time())
[1] "Q1"

format(Sys.time(), "%Y")
[1] "2017"

Round

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

Comparison

time1 lop time2

where:

Time Diff

julian(Sys.time())
Time difference of 17220.38 days

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"

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

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

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)

cut_point = seq(as.POSIXct("2017-06-06 00:30:00",tz="UTC"),by="30 min",length.out=48)
cut(x, breaks=cut_point)