3

I am not sure if this is just an output issue, but just wanted to check.

I have the following character string that I would like to convert to POSIXct, but unlike traditional POSIXct I have sub-second accuracy that I would like to maintain, as I would like to use it as an index for an xts object.

So the issues is as follows, I have character string object j

> dput(j)
c("2013-07-09 23:40:00.001", "2013-07-09 23:40:00.002", "2013-07-09 23:40:00.003", "2013-07-09 23:40:00.004")

and would like to covert it to a POSIXct object

so would normally try this:

> options("digits.secs" = 3)
> as.POSIXct(j, format="%Y-%m-%d %H:%M:%OS")
[1] "2013-07-09 23:40:00.000 BST" "2013-07-09 23:40:00.002 BST"
[3] "2013-07-09 23:40:00.003 BST" "2013-07-09 23:40:00.003 BST"

As you can see the output object doesn't quite match the character strings (i.e. the subseconds are 0,2,3,3 rather than 1,2,3,4)...is there a way to fix this, so that it does? or is it just an output visualisation issue?

h.l.m
  • 13,015
  • 22
  • 82
  • 169

1 Answers1

2

It is a representation error, combined with truncation.

Observe the output with 6 d.p.:

format(as.POSIXct(j),"%Y-%m-%d %H:%M:%OS6")
[1] "2013-07-09 23:40:00.000999" "2013-07-09 23:40:00.002000"
[3] "2013-07-09 23:40:00.003000" "2013-07-09 23:40:00.003999"
James
  • 65,548
  • 14
  • 155
  • 193