0

I am a little confused by the printing behavior of very large numeric numbers in R. Here is what I mean:

var1 = 122999998888
var1
[1] 1.23e+11

var2 = 123993239544
var2
[1] 123993239544

var1 < var2
[1] TRUE

It seems that it can not print a smaller numeric number exactly what it is as a larger one. How could this be?

class(var1)
[1] "numeric"
class(var2)
[1] "numeric"

Can somebody help illustrate this problem? This can be problematic if there are some large IDs represented as large numbers and when I read them in and write out as numeric data type, the ID will be different.

Psidom
  • 209,562
  • 33
  • 339
  • 356
  • 5
    "Numeric" values are stored as floating point number. Learn more about floating point numbers in R in this FAQ: http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal. If your large IDs are really just IDs with no numeric properties (adding them wouldn't make any sense) consider storing them as "character" values instead so values will never be rounded. – MrFlick May 02 '16 at 02:22
  • 2
    Well, you you've changed the question to be just about printing. R rounds values to make them look pretty on screen so what prints isn't always exactly what's stored. If you want to control when scientific notation is used when printing, look at the `scipen=` value in the `options()` function help page. – MrFlick May 02 '16 at 02:25
  • 1
    Check out `?print.default`. If you set the number of digits to print higher, `var1` will print everything (i.e. `print(var1, digits = 12)`). It hints as why the behavior is what it is, too, as `getOptions("digits")` defaults to 7. If you look at the seventh digit of `var1`, it would round to zero, so it chops back to where it wouldn't and prints in scientific notation. `var2` has a non-zero digit there, and rounding there wouldn't make it shorter. Regardless, the numbers are still stored, whether they print or not, but beware of floating point error if you calculate them. – alistaire May 02 '16 at 02:31
  • Excellent! Thanks @MrFlick I think `scipen` is the way to explain here. It seems that there is some underhood algorithm to decide how to print the numeric number which does not only depends on the value of the number. Thanks for the helpful comment @alistaire , even though the `digits` actually decides how the decimal numbers are printed instead of the case I am showing here. – Psidom May 03 '16 at 00:33

0 Answers0