290

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.

1

4 Answers 4

279

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).

5
  • 1
    Thanks. scipen seems to be the option I was looking for. The spooky penalty explanation made me shy away. But your example explains it nicely. sprintf, huh? are you referring to the troubles I with sprintf a week ago? :) Feb 22, 2012 at 15:41
  • 5
    In rstudio, if you import a dataset and do train_sample_10k = format(train_sample_10k,scientific=FALSE) and reload, it will change scientific notations.
    – mixdev
    Nov 23, 2014 at 5:21
  • How do I return things to normal after having done this?
    – ABIM
    Jan 5, 2018 at 19:34
  • 8
    @CSA: options("scipen"=0, "digits"=7) (those are the default values)
    – Scarabee
    Mar 1, 2018 at 1:47
  • You should move the one that achieves the result options("scipen"=100, "digits"=4) to the top of the code, and the one that doesn't below it ... with the appropriate notes. It can be confusing to someone who is looking for a quick solution (and Google shows the first one as the result).
    – xbsd
    Feb 11, 2019 at 21:51
207

It can be achieved by disabling scientific notation in R.

options(scipen = 999)
2
  • 5
    Moreover, this can be put in your .Rprofile file so it gets auto-executed by default.
    – smci
    Feb 13, 2018 at 1:16
  • If you want to do this just within a function then use withr::local_options(list(scipen = 999)) in the function code.
    – Patrick
    May 24 at 11:40
138

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

7
  • 2
    Hm that's weird, it doesn't work for me. I don't get an error, it just still prints sciencific notation.
    – Ovi
    Mar 26, 2018 at 7:06
  • 1
    Not sure what could be wrong. I checked in a very old (3.1.0) and new (3.4.3) version of R and it works for me in both. Most likely some other setting somewhere is taking precedence or you found a version specific or edge-case bug in R. Is it possible you are feeding it a string in scientific notation rather than a numeric object? That would explain it.
    – Danny
    Mar 26, 2018 at 17:08
  • 16
    Perhaps noteworthy that this creates a character instead of number.
    – cengel
    Mar 26, 2018 at 20:34
  • 4
    If the numbers in your vector are varying lengths, make sure to use justified = "none" or else there will be spaces padding them to the same length. Jul 2, 2018 at 20:08
  • 1
    format(1e6, scientific=FALSE) returns "1000000" while as.character(1e6) returns "1e+06", so there is a difference between the two methods.
    – mickey
    Dec 4, 2018 at 18:24
18

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
6
  • Why exactly the same answer? stackoverflow.com/a/27318351/680068 Apart from the Rprofile bit, maybe better edit the GingerJack's answer?
    – zx8754
    Feb 13, 2018 at 7:39
  • @zx8754: it's not exactly the same answer: the crucial point is move this stuff to your .Rprofile. Then you can never forget it. Also, as time goes by your .Rprofile accumulates all your customizations.
    – smci
    Feb 13, 2018 at 7:57
  • 1
    Up to you of course, but the Q is not "how can I not forget to do X" but "how can I do X".
    – zx8754
    Feb 13, 2018 at 8:00
  • @zx8754: I thunk between R and Python/pandas on multiple projects daily. Both have customizations, package paths etc. It really keeps things sane to have one common config file storing them. Across projects.
    – smci
    Feb 13, 2018 at 8:05
  • 1
    @zx8754: when you're working on multiple projects across multiple languages, the question "how can I do X" merges with "how can I not forget to do X", in a scalable, consistent, automatic way. I just added more explanation. For whoever the drive-by downvoter is.
    – smci
    Mar 27, 2018 at 8:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.