289

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.

Jonathan Callen
  • 11,301
  • 2
  • 23
  • 44
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207

4 Answers4

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

dash2
  • 2,024
  • 6
  • 15
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 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? :) – Matt Bannert Feb 22 '12 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 '14 at 05:21
  • How do I return things to normal after having done this? – ABIM Jan 05 '18 at 19:34
  • 8
    @CSA: `options("scipen"=0, "digits"=7)` (those are the default values) – Scarabee Mar 01 '18 at 01: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 '19 at 21:51
206

It can be achieved by disabling scientific notation in R.

options(scipen = 999)
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
GingerJack
  • 3,044
  • 1
  • 17
  • 19
  • 5
    Moreover, this can be put in your [.Rprofile file](https://csgillespie.github.io/efficientR/3-3-r-startup.html#rprofile) so it gets auto-executed by default. – smci Feb 13 '18 at 01: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 '23 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

Danny
  • 3,077
  • 2
  • 23
  • 26
  • 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 '18 at 07: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 '18 at 17:08
  • 16
    Perhaps noteworthy that this creates a character instead of number. – cengel Mar 26 '18 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. – Lauren Fitch Jul 02 '18 at 20:08
  • @LaurenFitch its actually `justify="none"` not `justified`, in either case I think its actually `trim=TRUE` that prevents the addition of padding whitespaces and not the`justify` flag – Parsa Nov 02 '18 at 16:01
  • 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 04 '18 at 18:24
  • This should be the accepted answer as it can be run without manipulation of a global option such as `scipen`. – ATpoint Jul 10 '21 at 12:10
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
smci
  • 32,567
  • 20
  • 113
  • 146
  • Why exactly the same answer? https://stackoverflow.com/a/27318351/680068 Apart from the Rprofile bit, maybe better edit the GingerJack's answer? – zx8754 Feb 13 '18 at 07: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 '18 at 07: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 '18 at 08: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 '18 at 08:05
  • Sorry, I am not questioning usefulness of putting stuff in *Rprofile*. Anyway, do as you wish, to me your answer looks the same as GingerJack's. Maybe, I am wrong. – zx8754 Feb 13 '18 at 08:08
  • 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 '18 at 08:29