0

I am trying to convert a chr into a number. The number I am trying to convert is "20171023063155.557". When I use the as.numeric function, it gives me 20171023063155.559. I have tried a few different methods but cannot get it to convert correctly.

Any help would be much appreciated.

as.POSIXct("20171023063155.557", format = "%Y%m%d%H%M%OS")
[1] "2017-10-23 06:31:55 PDT"
> as.POSIXct("20171023063155.557", format = "%Y%m%d%H%M%S")
[1] "2017-10-23 06:31:55 PDT"
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    is that a date? if so, you might want to try `as.POSIXct` – Ricardo Saporta Oct 25 '17 at 05:31
  • 3
    it the number is not interpreted as a date but as a "normal" number: The reason for the difference is explained here: https://stackoverflow.com/questions/22466328/how-to-work-with-large-numbers-in-r – R Yoda Oct 25 '17 at 05:34
  • Solution: Use `POSIXlt` instead: y <- as.POSIXlt("20171023063155.557", format = "%Y%m%d%H%M%OS"); format(y, "%Y-%m-%d %H:%M:%OS6") – R Yoda Oct 25 '17 at 06:10

2 Answers2

2

Your string actually appears to be a timestamp. I would therefore suggest that you treat it as such. One option here would be to convert it to a date using as.POSIXct:

x <- "20171023063155.557"
y <- as.POSIXct(x, format = "%Y%m%d%H%M%OS")

With a POSIXct object in hand, you can now easily extract information about your timestamp, e.g.

weekdays(y, FALSE)
months(y, FALSE)
[1] "Monday"
[1] "October"

To verify that millisecond precision information has in fact been stored in the POSIXct object, we can call format to check:

format(y, "%Y-%m-%d %H:%M:%OS6")
[1] "2017-10-23 06:31:55.556999"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Correct, it is a timestamp. This code however does not keep the milliseconds, which is important for my results. Any thoughts? – user8829384 Oct 25 '17 at 05:37
  • So are you saying it actually looks something like this: `20171023063155` ? If so, then just change the format mask to this: `%Y%m%d%H%M%S`. – Tim Biegeleisen Oct 25 '17 at 05:39
  • 1
    I edited the question and used the function you gave me. Both ways, I do not get milliseconds. – user8829384 Oct 25 '17 at 05:44
  • 2
    I think the issue is that milliseconds don't display when you print the `POSIXct` object, but they are there. Try `format(y, "%Y%m%d%H%M%OS4")`. – neilfws Oct 25 '17 at 05:48
  • @user8829384 Check my edit. The millisecond precision certainly is there. If your issue is not _seeing_ that precision, then you have another problem. – Tim Biegeleisen Oct 25 '17 at 05:49
  • Anyway it could give me a 7 instead of the 6999? – user8829384 Oct 25 '17 at 05:54
  • 1
    Why are you worried about such fine precision? In any case, floating point arithmetic is not exact in many programming languages (e.g. C). This sort of thing is to be expected AFAIK. – Tim Biegeleisen Oct 25 '17 at 05:56
  • @user8829384 Since dates as POSICct are represented as `double` floating point number you cannot fix the inherent problems of floating point numbers even if you the number as a date (see my answer to explain the problem) – R Yoda Oct 25 '17 at 05:57
0

The problem is the the "rounding" difference imposed by using 32 bit floating point number (class float) and the default number of significant digits R is configured to print:

x <- as.numeric("20171023063155.557")
x
# [1] 2.017102e+13
getOption("digits")
# 7
options(digits=22)
x
# [1] 20171023063155.55859375

So just change the digits option and you can see your number is converted (almost ;-) correctly...

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • I guess there is no function that would convert it exactly? – user8829384 Oct 25 '17 at 05:57
  • 1
    Floating point arithmetic is not necessarily exact. Maybe you should edit your question and let us know why this matters to your application. – Tim Biegeleisen Oct 25 '17 at 05:57
  • Perhaps you could tweak some floating point options (see `?.Machine` but in the end this is a technical limitation not of R but any programming language if you use floating point numbers. So yes, I think there is generally no way to convert it exactly :-( – R Yoda Oct 25 '17 at 05:58
  • I don't know, I'm just anal – user8829384 Oct 25 '17 at 05:59