Preserving large numbers

James picture James · May 23, 2012 · Viewed 24.5k times · Source

I am trying to read a CSV file that has barcodes in the first column, but when R gets it into a data.frame, it converts 1665535004661 to 1.67E+12.

Is there a way to preserve this number in an integer format? I tried assigning a class of "double", but that didn’t work, nor did assigning a class of "character". Once it is in the 1.67E+12 format any attempt to convert it back to an integer returns 167000000000.

Answer

John picture John · May 23, 2012

It's not in a "1.67E+12 format", it just won't print entirely using the defaults. R is reading it in just fine and the whole number is there.

x <- 1665535004661
> x
[1] 1.665535e+12
> print(x, digits = 16)
[1] 1665535004661

See, the numbers were there all along. They don't get lost unless you have a really large number of digits. Sorting on what you brought in will work fine and you can just explicitly call print() with the digits option to see your data.frame instead of implicitly by typing the name.