R: Numeric representation of date time
I’ve been playing around with date times in R recently and I wanted to derive a numeric representation for a given value to make it easier to see the correlation between time and another variable.
e.g. December 13th 2014 17:30 should return 17.5 since it’s 17.5 hours since midnight.
Using the standard R libraries we would write the following code:
> december13 = as.POSIXlt("2014-12-13 17:30:00")
> as.numeric(december13 - trunc(december13, "day"), units="hours")
[1] 17.5
That works pretty well but Antonios recently introduced me to the lubridate so I thought I’d give that a try as well.
The first nice thing about lubridate is that we can use the date we created earlier and call the floor_date function rather than truncate:
> (december13 - floor_date(december13, "day"))
Time difference of 17.5 hours
That gives us back a difftime...
> class((december13 - floor_date(december13, "day")))
[1] "difftime"
...which we can divide by different units to get the granularity we want:
> diff = (december13 - floor_date(december13, "day"))
> diff / dhours(1)
[1] 17.5
> diff / ddays(1)
[1] 0.7291667
> diff / dminutes(1)
[1] 1050
Pretty neat!
lubridate also has some nice functions for creating dates/date times. e.g.
</p>
> ymd_hms("2014-12-13 17:00:00")
[1] "2014-12-13 17:00:00 UTC"
> ymd_hm("2014-12-13 17:00")
[1] "2014-12-13 17:00:00 UTC"
> ymd_h("2014-12-13 17")
[1] "2014-12-13 17:00:00 UTC"
> ymd("2014-12-13")
[1] "2014-12-13 UTC"
And if you want a different time zone that’s pretty easy too:
> with_tz(ymd("2014-12-13"), "GMT")
[1] "2014-12-13 GMT"
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.