[R] as.numeric looses precision
MacQueen, Don
macqueen1 at llnl.gov
Thu Jul 2 16:50:21 CEST 2015
Although Kehl and Petr have already answered the question, I would suggest
this as the simplest way to understand what is going on:
> p <- "1087.003489"
> print( as.numeric(p) , digits=16)
[1] 1087.003489
When R prints numbers, it follows various rules regarding how many decimal
places to display. You assumed it would print all six decimal places, but
that was not the case. The were there in the number itself, but not
printed.
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 7/2/15, 6:54 AM, "R-help on behalf of Bos, Roger"
<r-help-bounces at r-project.org on behalf of roger.bos at rothschild.com> wrote:
>I have a string that contains a number and when I convert it to a number
>I loose precision and I would like to know if there is a way to avoid
>that. Here is my example:
>
>p <- "1087.003489"
>as.numeric(p, digits=6)
>
>R gives me 1087.003:
>
>> p <- "1087.003489"
>> as.numeric(p, digits=6)
>[1] 1087.003
>
>I would be nice if I could keep all the decimal places after the
>conversion.
>
>Thanks,
>
>Roger
>
>
>
>
>
>***************************************************************
>This message and any attachments are for the intended recipient's use
>only.
>This message may contain confidential, proprietary or legally privileged
>information. No right to confidential or privileged treatment
>of this message is waived or lost by an error in transmission.
>If you have received this message in error, please immediately
>notify the sender by e-mail, delete the message, any attachments and all
>copies from your system and destroy any hard copies. You must
>not, directly or indirectly, use, disclose, distribute,
>print or copy any part of this message or any attachments if you are not
>the intended recipient.
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list