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
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
Hi Roger,
You're mixing up storage and display:
p <- "1087.003489"
p <- as.numeric(p)
> p
[1] 1087.003 # the default value for digits = 7, which can be changed
with option()
> print(p, digits=4)
[1] 1087
> print(p, digits=10)
[1] 1087.003489
But note also:
> print(p, digits=20)
[1] 1087.003488
ue.
"1087.003489" number can not be represented precisely in binary arithmetic.
Cheers
Petr
> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Bos,
> Roger
> Sent: Thursday, July 02, 2015 3:55 PM
> To: r-help@r-project.org
>
To: r-help@r-project.org
Tárgy: [R] as.numeric looses precision
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 give
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
6 matches
Mail list logo