Function write() inconsistent with number notation

Consider the following script:

list_of_numbers <- as.numeric() for(i in 1001999498:1002000501){ list_of_numbers <- c(list_of_numbers, i) } write(list_of_numbers, file = "./list_of_numbers", ncolumns = 1)

The file that is produced looks like this:

[user@pc ~]$ cat list_of_numbers 1001999498 1001999499 1.002e+09 ... 1.002e+09 1.002e+09 1.002e+09 1002000501

I found a couple more ranges where R does not print consistently the number format.

Now I have the following questions:

Is this a bug or is there an actual reason for this behavior? Why just in certain ranges, why not every number above x?

I know how I can solve this like this:

options(scipen = 1000)

But are there more elegant ways than setting global options? Without converting it to a dataframe and changing the format.


It's not a bug, R chooses the shortest representation.

More precisely, in ?options one can read:

fixed notation will be preferred unless it is more than scipen digits wider.

So when scipen is 0 (the default), the shortest notation is preferred.

Note that you can get the scientific notation of a number x with format(x, scientific = TRUE).

In your case:

    <li>1001999499 is 10 characters long whereas its scientific notation 1.001999e+09 is longer (12 characters), so the decimal notation is kept.</li> <li>1001999500: scientific notation is 1.002e+09, which is shorter.</li> <li>..................... (scientific notation stays equal to 1.002e+09, hence shorter)</li> <li>1002000501: 1.002001e+09 is longer.</li> </ul>

    You may ask: how come that 1001999500 is formatted as 1.002e+09 and not as 1.0019995e+09? It's simply because there is also an option that controls the number of significant digits. It is named digits and its default value is 7. Since 1.0019995 has 8 significant digits, it is rounded up to 1.002.

    The simplest way to ensure that decimal notation is kept without changing global options is probably to use format:

    write(format(list_of_numbers, scientific = FALSE, trim = TRUE), file = "./list_of_numbers") <hr>

    Side note: you didn't need a loop to generate your list_of_numbers (which by the way is not a list but a vector). Simply use:

    list_of_numbers <- as.numeric(1001999498:1002000501)


  • Forcing R output to be scientific notation with at most two decimals
  • Program type already present: com.squareup.okhttp.Address
  • Why does x86 program segfault without .data section? [duplicate]
  • FParsec: how to combine parsers so that they will be matched in arbitrary order
  • Aspect ratio behaviour of - internal dimensions and element size
  • Level-order tree traversal
  • How to get to older Xcode beta version?
  • Safari PHP form submission -file upload hangs
  • Is there an easy way to associate an event with a ListViewItem?
  • Typecasting `this` of a base class template to its derived class
  • Bound DataGridViewCheckBoxColumn not updating binding source
  • Create function that can pass a parameter without making a new component
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • Compare struct to a constant in C
  • How do I mock an exported typescript function in a jasmine test?
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • SyntaxError: (irb):26: both block arg and actual block given
  • Salesforce Different WSDL files and when to use
  • Using Sax parsing to edit and write XML in VB6
  • Android application: how to use the camera and grab the image bytes?
  • Sequential (transactional) API calls in angular 4 with state management
  • How does this usort cmp function actually work?
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Debug.DrawLine not showing in the GameView
  • Time complexity of a program which involves multiple variables
  • Yii2: Config params vs. const/define
  • Checking free space on FTP server
  • QLineEdit password safety
  • Deleting and Updating values from a cusrsor adapter
  • RectangularRangeIndicator format like triangular using dojo
  • Modifying destination and filename of gulp-svg-sprite
  • Javascript Callbacks with Object constructor
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • ORA-29908: missing primary invocation for ancillary operator
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Matrix multiplication with MKL
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Is it possible to post an object from jquery to bottle.py?
  • Python/Django TangoWithDjango Models and Databases