149

I'm plotting and performing calculations on uniformly distributed time series. The timestamps are currently stored as integers representing the number of seconds since the UNIX epoch (e.g. 1352068320), but Date objects seem more appropriate for plotting. How can I do the conversion?

I've read ?Date, ?as.Date and ??epoch, but seem to have missed that information.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Andreas
  • 7,470
  • 10
  • 51
  • 73

2 Answers2

259

Go via POSIXct and you want to set a TZ there -- here you see my (Chicago) default:

R> val <- 1352068320
R> as.POSIXct(val, origin="1970-01-01")
[1] "2012-11-04 22:32:00 CST"
R> as.Date(as.POSIXct(val, origin="1970-01-01"))
[1] "2012-11-05" 
R> 

Edit: A few years later, we can now use the anytime package:

R> library(anytime)
R> anytime(1352068320)
[1] "2012-11-04 16:32:00 CST"
R> anydate(1352068320)
[1] "2012-11-04"
R> 

Note how all this works without any format or origin arguments.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 11
    I have timestamps like `1415560016876`. http://www.epochconverter.com/ converts this to a date with no problem. Your code above gives me stuff like `"46832-11-09 12:47:33 EDT"`... – Hack-R Nov 17 '14 at 19:43
  • 37
    Try dividing that by 1000: `as.POSIXct(1415560016876/1000, origin="1970-01-01")` gets "2014-11-09 13:06:56.875 CST" and you need to ensure whether _seconds_ are expected (as for R) or _milliseconds_. – Dirk Eddelbuettel Nov 17 '14 at 19:47
  • @DirkEddelbuettel: Thanks! Is there a guide about when to divide by 1000 and when not to?? Just trying to understand the basics. – Shambho Feb 03 '15 at 01:13
  • 3
    @Shambho: Just do the reverse and see if you're in the same order of magnitude: `print(as.numeric(Sys.time()))` – Dirk Eddelbuettel Feb 03 '15 at 02:16
  • I have the same case, but I need the millisecond data, so I can't divide by 1000. Any ideas on how to represent these as dates? – Omri374 May 25 '15 at 12:00
  • 2
    It's always the same: scale what you have to that it arrives at the same _scale_ as the current time: `print(as.numeric(Sys.time()), digits=16)` with the six digits is what my Linux system. Also, you *can* divide by 1000; this does not truncate. – Dirk Eddelbuettel May 25 '15 at 12:06
  • 1
    How would you extract just the local time from the R variable, and dump the date? – Stratix Oct 21 '15 at 19:41
  • Look at the available print formats, eg via `help(strptime)`. – Dirk Eddelbuettel Oct 21 '15 at 19:56
34

With library(lubridate), numeric representations of date and time saved as the number of seconds since 1970-01-01 00:00:00 UTC, can be coerced into dates with as_datetime():

lubridate::as_datetime(1352068320)

[1] "2012-11-04 22:32:00 UTC"
Yuriy Barvinchenko
  • 1,465
  • 1
  • 12
  • 17
  • Strictly speaking this is not correct as it isn't _lubridate_ which has the "numeric represenation [...] as number of seconds" but _base R_ just as my four-years older answer already showed -- so what we have here is a _lubridate_ alternative to the two commands I already showed earlier. – Dirk Eddelbuettel Jan 31 '21 at 21:22
  • Dear @DirkEddelbuettel, Thank you for feedback. Yes, this is -lubridate- alternative. My answer could be re-edited as: Numeric representations of date and time saved as the number of seconds since 1970-01-01 00:00:00 UTC, can be coerced into dates with lubridate::as_datetime() – Yuriy Barvinchenko Feb 01 '21 at 15:43
  • Sure. As they can with `as.POSIXct` (needing an origin) or `anytime`. – Dirk Eddelbuettel Feb 01 '21 at 16:24
  • I see. Corrected – Yuriy Barvinchenko Feb 01 '21 at 16:42