SoFunction
Updated on 2025-04-10

Introduction to R language scientific counting method: digits and scipen settings

The scientific algorithm of controlling R language shows two options: digitis and scipen. There is very little information introduced, and some are wrong. After looking through the help of R language and carefully considering the examples, the summary is as follows:

The default settings are:

getOption("digits")
[1] 7
getOption("scipen")
[1] 0

digits The number of valid numeric characters is 7 by default, and the range is [1,22]

scipen The penalty displayed by scientific count can be positive or negative, and the default is 0.

When R outputs numbers, the length represented by ordinary numbers is <= Character length represented by scientific notation + scipen length is retained, otherwise the length represented by scientific notation is used.

Take a chestnut:

&gt; options(digits = 2) # The valid number is 2 digits&gt; options(scipen = 1)
&gt; 1         # 1e+00 Length is 5, 1 reserved for display, length is 1[1] 1
&gt; 12345678   # 1.2e+07, length is 7, 7 + scipen = 8, the normal number represents the length is 8, and does not exceed 8, and the representation of different numbers is retained.[1] 12345678
&gt; 123456789   # 1.2e+08, length is 7, 7 + scipen =8, ordinary numbers represent length is 9, so switch to scientific notation representation[1] 1.2e+08

A simple method (not so accurate, for example, when digits=1, there is no decimal point; when the number is very large, the exponent may be 3 digits) can estimate the longest string of numbers like this:

digits + 1 (decimal point) + 4 (e+XX scientific notation) + scipen

For example, the longest number length that is not represented by scientific notation is 2+1+4+1 = 8

Let's see if the modification scipen = -2, verify whether the longest number length is 2+1+4 - 2 = 5

> options(scipen = -2)
> 1234
[1] 1234
> 12345
[1] 12345
> 123456
[1] 1.2e+05

really!

Supplement: R language sets the numerical output (retained to the decimal point and retain valid numbers)

In R language, the output of a number is 7 bits by default:

&gt; a = 0.1234567890   #10&gt; a
[1] 0.1234568

Note: The output results are rounded.

1 options(digits) function

Set the output length through the options(digits) function, when digits = 3:

&gt; options(digits = 3) 
&gt; a = 0.1234567890 #10&gt; a
[1] 0.123

When digits = 10:

&gt; options(digits = 10)
&gt; a = 0.1234567890   #10&gt; a
[1] 0.123456789

digits can be 22 at most, and if it exceeds 22, an error will be reported:

> options(digits = 3)
> options(digits = 22)
> options(digits = 23)
Error in options(digits = 23) : 
  invalid 'digits' parameter, allowed 0...22

The output result is only 9 bits, and the end 0 is omitted.

2 round(x, n) function

In the round(x, n) function, x is a number and n is the number of digits reserved after the decimal point. When setting n = 4:

&gt; a = 0.1234567890   #10&gt; round(a, 4)
[1] 0.1235
&gt; a = 1.234567890   #9 decimal places&gt; round(a, 4)
[1] 1.2346

Note: The output results are rounded.

When setting n = 10:

&gt; a = 0.1234567890   #10&gt; round(a, 10)
[1] 0.123456789

The output result is only 9 bits, and the end 0 is omitted.

When the number of 0 after the decimal point exceeds n, the output result is 0:

&gt; a = 0.0001234567890   #13&gt; round(a, 3)
[1] 0
&gt; a = 0.0001234567890   #13&gt; round(a, 4)
[1] 1e-04

3 signif(y, n) function

In the signif(x, n) function, x is a number and n is the number of significant numbers. When n = 4:

&gt; a = 1.234567890   #9 decimal places&gt; signif(a, 4)
[1] 1.235
&gt; a = 0.000001234567890   #15 digits after the decimal point&gt; signif(a, 4)
[1] 1.235e-06

When n = 10:

&gt; a = 1.234567890   #9 decimal places&gt; signif(a, 10)
[1] 1.23456789

At this time, the 0 at the end of the number is still omitted.

4 sprintf(fmt, …) function

&gt; a = 0.1234567890   #10 digits after the decimal point&gt; sprintf("%0.4f", a)
[1] "0.1235"
&gt; a = 0.1234567890   #10 digits after the decimal point&gt; sprintf("%0.10f", a)
[1] "0.1234567890"

The 0 at the end can be preserved through the sprintf(fmt, ...) function.

When the input is an integer, the number of digits is insufficient and 0 will be added before the input value:

> a = 12456789
> sprintf("%03d", a)
[1] "12456789"
> a = 12
> sprintf("%03d", a)
[1] "012"

Everyone is welcome to criticize and correct me.

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.