Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R floating point number precision being lost on coversion from character

Tags:

casting

r

numeric

I have a large floating point number as a character like so

  x<-"5374761693.91823";

On doing

 as.numeric(x); 

I get the following output

   5374761694

I would like to preserve the floating point nature of the number while casting.

like image 589
Aditya Sihag Avatar asked Oct 03 '22 10:10

Aditya Sihag


1 Answers

use digits argument in print to see the actual number:

> print(as.numeric(x), digits=15)
[1] 5374761693.91823

options is another alternative:

> options(digits=16)
> as.numeric(x)
[1] 5374761693.91823

> # assignments
> options(digits=16)
> y <- as.numeric(x)
> y
[1] 5374761693.91823

z <- print(as.numeric(x), digits=15)
z
like image 87
Jilber Urbina Avatar answered Oct 11 '22 15:10

Jilber Urbina