Handling big numbers in R

One of the issue with R big (long numbers). You can find very detail explanation on what is the issue. But in this post we discuss in a very simple way. Mainly how to work around when faced with this issue.

A simple example

Let's say need to read text file with varying length numbers.


number
1234567891234567
123456789123456789
9988776655443322111111
54767686587697697696679565664656
2423546567899009052342313244568798978
1000000000000000006666666666660000000000000003333333333


Using red csv

aa <- read.csv("C:\\data\\rblog\\number.txt")


What we get is this.



Checking the length....

nchar(aa$number)
[1] 16 18 20 20 20  5


The numbers are automatically changed to scientific formatting.

To avoid scientific formatting you can use,

options(scipen=999)






First looks it seems to be OK, but if you look at the last digits you can see they are changed, except for the first row.


To avoid this we need to force the data as character. 

aa <- read.csv("C:\\data\\rblog\\number.txt",  colClasses= 'character')

Then we get.




If we force the character to convert to numeric,

aa$number <- as.numeric(aa$number)

The issue of changing last digits will reappear. 





No comments:

Post a Comment