[R] as.numeric looses precision

Calin Uioreanu prophp at gmail.com
Thu Jul 2 16:05:19 CEST 2015


Hello,

There's a difference between the displayed value and the internally stored.
You could use options(digits=10), but this is a global option, and will
affect all future numerical output. default value is 7

> p <- "1087.003489"
> as.numeric(p, digits=6)
[1] 1087.003
> options(digits=10) # set output precision
> as.numeric(p, digits=6)
[1] 1087.003489
>

for more information look here:
http://stackoverflow.com/questions/4540649/retain-numerical-precision-in-an-r-data-frame

regards calin


On Thu, Jul 2, 2015 at 3:56 PM Bos, Roger <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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list