R: Numeric Representation of Date Time
Join the DZone community and get the full member experience.
Join For FreeI’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.
> 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"
Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
What Is Istio Service Mesh?
-
File Upload Security and Malware Protection
-
Logging Best Practices Revisited [Video]
-
Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
Comments