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

4

291

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.

Sandhog answered 22/2, 2012 at 15:24 Comment(1)
R
281

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

Rodney answered 22/2, 2012 at 15:33 Comment(5)
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? :)Sandhog
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.Whereabouts
How do I return things to normal after having done this?Scarcely
@CSA: options("scipen"=0, "digits"=7) (those are the default values)Tripterous
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).Instinct
P
211

It can be achieved by disabling scientific notation in R.

options(scipen = 999)
Placement answered 5/12, 2014 at 14:43 Comment(2)
Moreover, this can be put in your .Rprofile file so it gets auto-executed by default.Hollyhock
If you want to do this just within a function then use withr::local_options(list(scipen = 999)) in the function code.Kiss
G
141

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

Glume answered 21/12, 2017 at 21:2 Comment(7)
Hm that's weird, it doesn't work for me. I don't get an error, it just still prints sciencific notation.Dichotomous
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.Glume
Perhaps noteworthy that this creates a character instead of number.Louralourdes
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.Pervious
@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 thejustify flagGoldina
format(1e6, scientific=FALSE) returns "1000000" while as.character(1e6) returns "1e+06", so there is a difference between the two methods.Eliseo
This should be the accepted answer as it can be run without manipulation of a global option such as scipen.Zoogloea
H
19

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
Hollyhock answered 13/2, 2018 at 1:16 Comment(6)
Why exactly the same answer? https://mcmap.net/q/88323/-force-r-not-to-use-exponential-notation-e-g-e-10 Apart from the Rprofile bit, maybe better edit the GingerJack's answer?Hettie
@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.Hollyhock
Up to you of course, but the Q is not "how can I not forget to do X" but "how can I do X".Hettie
@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.Hollyhock
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.Hettie
@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.Hollyhock

© 2022 - 2024 — McMap. All rights reserved.