Force R Not to Use Exponential Notation (E.G. E+10)

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

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

Prevent as.character from using exponential notation r

format is the function that lets you choose how you want your numbers formatted when converting to character. In this case, something like

format(c(9999999, 10000000), scientific = FALSE, trim = TRUE)
#> [1] "9999999" "10000000"

How to prevent scientific notation in R?

To set the use of scientific notation in your entire R session, you can use the scipen option. From the documentation (?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.

So in essence this value determines how likely it is that scientific notation will be triggered. So to prevent scientific notation, simply use a large positive value like 999:

options(scipen=999)

Convert scientific notation to numeric, preserving decimals

You can turn off scientific notation for numbers using the option below;

options(scipen = 999)

That would make all the numbers to appear as decimals.


If you want to revert it back to the default, use

options(scipen = 0)

See getOption("scipen") for more options.

Force R to stop plotting abbreviated axis labels (scientific notation) - e.g. 1e+00

I think you are looking for this:

require(ggplot2)
df <- data.frame(x=seq(1, 1e9, length.out=100), y=sample(100))
# displays x-axis in scientific notation
p <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
p

# displays as you require
require(scales)
p + scale_x_continuous(labels = comma)

Disable Exponential Notation when printing with fwrite r

If you look at the source code of fwrite() function it passes the values your values straight to internal C function:

> fwrite
function (x, file = "", append = FALSE, quote = "auto", sep = ",",
sep2 = c("", "|", ""), eol = if (.Platform$OS.type == "windows") "\r\n" else "\n",
na = "", dec = ".", row.names = FALSE, col.names = TRUE,
qmethod = c("double", "escape"), logicalAsInt = FALSE, dateTimeAs = c("ISO",
"squash", "epoch", "write.csv"), buffMB = 8, nThread = getDTthreads(),
showProgress = getOption("datatable.showProgress"), verbose = getOption("datatable.verbose"))
{
...
.Call(Cwritefile, x, file, sep, sep2, eol, na, dec, quote,
qmethod == "escape", append, row.names, col.names, logicalAsInt,
dateTimeAs, buffMB, nThread, showProgress, verbose)
invisible()
}

If you look at the source code of the function that is called:
https://github.com/Rdatatable/data.table/blob/master/src/fwrite.c
you will notice that they do not check for any environment set in R and use significant notation for large enough values.
One can change this source the way you like, build own dynamic library and call it from R.
The other option would be to use some standard R writing functions (though I suspect you like the performance of data.table package functions).



Related Topics



Leave a reply



Submit