Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using scientific notation in R

Tags:

format

r

I'm currently using printCoefmat to print a matrix out and want to apply some formatting to the numbers.

I want to force scientific notation when the numbers have an exponent greater than 3. I can't quite figure out how scipen works, Does anyone have any idea how I can do this?

like image 882
Phyx Avatar asked May 03 '13 10:05

Phyx


People also ask

How do you avoid exponential formatting in R?

In order to eliminate the exponential notation of the integer, we can use the global setting using options() method, by setting the scipen argument, that is options(scipen = n). Scipen: A penalty to be applied when deciding to print numeric values in fixed or exponential notation.

What does Scipen mean in R?

The value of scipen is the number of orders of ten smaller (i.e. decimal places preceded by 0's) required to switch to scientific notation. Decreasing the value of scipen will cause R to switch to scientific location for larger numbers.

How do I turn a number into scientific notation?

A number is written in scientific notation when a number between 1 and 10 is multiplied by a power of 10. For example, 650,000,000 can be written in scientific notation as 6.5 ✕ 10^8.


1 Answers

Just type in a big number to get R to display unscientific notation.

options( scipen = 20 )

If that's not enough, make the number bigger...

How does the scipen penalty work?

It is confusing, but the penalty is applied to the scientific notation version, as in R looks at how many characters it takes to print a particular string. It adds the value scipen penalty to the number of characters in scientific notation and if it is still less than the number of characters required to print the actual number then it will print scientific and vice versa. I hope this example will illustrate the point:

options( scipen = 0 )
options( digits = 6 )
>1e5
#[1] 1e+05    ----> 5 characters in scientific, vs. 6 for '100000' in normal
>1e4
#[1] 10000    ----> 5 characters in normal, vs. 5 for '1e+04' in scientific
options(scipen = 1 )
>1e5
#[1] 100000    ----> 6 characters in normal, vs. 5 + 1 for '1e+05' + scipen penalty in scientific
like image 175
Simon O'Hanlon Avatar answered Sep 29 '22 11:09

Simon O'Hanlon