1

I have a problem in R with changing seconds with decimal places into a date.

Example:

seconds <- c(1124925778.888, 1124925778.889)
> as.POSIXct(seconds, origin = "1970-01-01 00:00:00.000")
[1] "2005-08-25 01:22:58.888 CEST" "2005-08-25 01:22:58.888 CEST"

As you can see these two numbers (which represent seconds) have different decimal places but are shown as this same date.

Could you tell me why it happens and how to parse it correctly?

My version of R is 3.4.3.

Jo.Hen
  • 55
  • 6
  • I wonder if this might be a floating point issue, as it seems to work "correctly" if you change it to different numbers that are still just 0.001 apart. – joran May 25 '18 at 17:51
  • Try using as.POSIXlt – cirofdo May 25 '18 at 17:53
  • `as.POSIXlt(seconds, origin = "1970-01-01 00:00:00.000")` – cirofdo May 25 '18 at 17:53
  • Precision issues while calculating datetime: Try this : `seconds <- c(1124925778.8880, 1124925778.8890) ; op <- options(digits.secs = 4) ; date_time <- as.POSIXlt(seconds, origin = "1970-01-01 00:00:00.000" ) ; strftime(date_time, "%d/%m/%y %H:%M:%OS") ; options(op)` – Mankind_008 May 25 '18 at 18:26

0 Answers0