R - Date Time (POSIXct)

Card Puncher Data Processing

About

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

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

Management

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

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") 

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

  • 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"

Diff

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

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"

Round

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

Comparison

time1 lop time2

where:

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

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

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)

  • 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)





Discover More
Ggplot Request Frequency Diagram
Ggplot - Histogram (geom_histogram, geom_freqpoly)

geom_histogram = stat_bin + geom_bar + position = “stacked” geom_histogramgeom_freqpoly = stat_bin + geom_line + position = “identity” The below code handles outliers by: creating manually...
Ggplot Graphic Plot
Ggplot - Time

Ggplot supports: the date class the PosixCt class (DateTime) the hms class (Only the time part) : for date_breaks, and date_format you need package scales:
Card Puncher Data Processing
R - Date

See also: Conversion Functions to and from Character Get Current date
Card Puncher Data Processing
R - Time

Note: Time can be represented with the following class: Date: Datetime (Date with Time (hour/minute/seconds): POSIXct times (hour, minute, ss): hms



Share this page:
Follow us:
Task Runner