Force R not to use exponential notation (e.g. e+10)?

RNumber FormattingOptionsScientific NotationDefaults

R Problem Overview


Can I force R to use regular numbers instead of using the e+10-like notation? I have:

1.810032e+09
# and 
4

within the same vector and want to see:

1810032000
# and
4

I am creating output for an old fashioned program and I have to write a text file using cat. That works fine so far but I simply can't use the e+10 notation there.

R Solutions


Solution 1 - R

This is a bit of a grey area. You need to recall that R will always invoke a print method, and these print methods listen to some options. Including 'scipen' -- a penalty for scientific display. From help(options):

> ‘scipen’: integer. A penalty to be applied when deciding to print > numeric values in fixed or exponential notation. Positive > values bias towards fixed and negative towards scientific > notation: fixed notation will be preferred unless it is more > than ‘scipen’ digits wider.

Example:

R> ran2 <- c(1.810032e+09, 4) 
R> options("scipen"=-100, "digits"=4)
R> ran2
[1] 1.81e+09 4.00e+00
R> options("scipen"=100, "digits"=4)
R> ran2
[1] 1810032000          4

That said, I still find it fudgeworthy. The most direct way is to use sprintf() with explicit width e.g. sprintf("%.5f", ran2).

Solution 2 - R

It can be achieved by disabling scientific notation in R.

options(scipen = 999)

Solution 3 - R

My favorite answer:

format(1810032000, scientific = FALSE)
# [1] "1810032000"

This gives what you want without having to muck about in R settings.

Note that it returns a character string rather than a number object

Solution 4 - R

Put options(scipen = 999) in your .Rprofile file so it gets auto-executed by default. (Do not rely on doing it manually.)

(This is saying something different to other answers: how?

  1. This keeps things sane when you thunk between multiple projects, multiple languages on a daily or monthly basis. Remembering to type in your per-project settings is error-prone and not scalable. You can have a global ~/.Rprofile or per-project .Rprofile. Or both, with the latter overriding the former.
  2. Keeping all your config in a project-wide or global .Rprofile auto-executes it. This is useful for e.g. default package loads, data.table configuration, environment etc. Again, that config can run to a page of settings, and there's zero chance you'll remember those and their syntax and type them in

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt BannertView Question on Stackoverflow
Solution 1 - RDirk EddelbuettelView Answer on Stackoverflow
Solution 2 - RGingerJackView Answer on Stackoverflow
Solution 3 - RDannyView Answer on Stackoverflow
Solution 4 - RsmciView Answer on Stackoverflow