Forcing R Output to Be Scientific Notation with at Most Two Decimals

Forcing R output to be scientific notation with at most two decimals

I think it would probably be best to use formatC rather than change global settings.

For your case, it could be:

numb <- c(0.05, 0.05671, 0.000000027)
formatC(numb, format = "e", digits = 2)

Which yields:

[1] "5.00e-02" "5.67e-02" "2.70e-08"

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)

Using format() in R to convert numeric value to scientific notation after rounding

I think rounded_vals might have been stored as character values. Try converting them to numbers using as.numeric() and then put it into the format() function.

Avoid scientific characters in R output files

  print( format(283187433.2, nsmall=1), quote=FALSE)
#[1] 283187433.2

Ouput to the console but this would also be what you saw in a text file.

write.table(file="", 283187433.2, quote=FALSE, row.names=FALSE, col.names=FALSE)
283187433.2

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

Format scientific notation in R

formatC(a, format = "e", digits = 2)

Scientific notation with Rmpfr in R

use scipen=0

   library(Rmpfr)
options(scipen = 999)
ns <- mpfr(1:24, 120) ; factorial(ns)
# 24 'mpfr' numbers of precision 120 bits
# [1] 1e0 2e0 6e0
# [4] 2.e1 1.e2 7.e2
# [7] 5.0e3 4.03e4 3.628e5
# [10] 3.628e6 3.9916e7 4.79001e8
# [13] 6.227020e9 8.7178291e10 1.30767436e12
# [16] 2.092278988e13 3.5568742809e14 6.40237370572e15
# [19] 1.2164510040883e17 2.4329020081766e18 5.10909421717094e19
# [22] 1.1240007277776076e21 2.58520167388849766e22 6.204484017332394393e23

options(scipen = 0)
ns <- mpfr(1:24, 120) ; factorial(ns)
# 24 'mpfr' numbers of precision 120 bits
# [1] 1 2 6
# [4] 24 120 720
# [7] 5040 40320 362880
# [10] 3628800 39916800 479001600
# [13] 6227020800 87178291200 1307674368000
# [16] 20922789888000 355687428096000 6402373705728000
# [19] 121645100408832000 2432902008176640000 51090942171709440000
# [22] 1124000727777607680000 25852016738884976640000 620448401733239439360000


Related Topics



Leave a reply



Submit