Skip to content
Related Articles

Related Articles

Improve Article

How to prevent scientific notation in R?

  • Last Updated : 16 May, 2021

Scientific notations in R Programming Language are considered equivalent to the exponential formats. Scientific notations are used for numbers that are extremely large or small to be handled in the decimal form. In R Language, the E-notation is used for such numbers. In this article, we are going to see how to prevent scientific notation in R programming.

Method 1: Using scipen option

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. Positive values bias towards fixed and negative towards scientific notation: fixed notation will be preferred unless it is more than scipen digits wider.

The scipen value is an indicator of the integer’s prompt for exponential notation. In order to prevent the triggering of scientific notation, any large positive number can be used, for all practical purposes. However, this method makes changes to the entire R configurational settings.



options(scipen=999)

This option can be reset by using 0 as the scipen value. 

Code:

R




num = 12383828281831342
print ("orignal number :")
print (num)
  
# after global setting for
# options 
options(scipen = 100, digits = 4)
  
# declaring the number
num = 12383828281831342
print ("Modified number :")
print (num)

Output:

[1] "orignal number :"
[1] 1.238383e+16
[1] "Modified number :"
[1] 12383828281831342

Method 2: Using format() method

In order to disable the scientific option for any function output or numerical integer, the format method in R can be customized. Modification is made to the original integer. This eliminates the automatic exponential representation of the number. This method can be used in case, we wish to eliminate the scientific notation for a specific number and not set it globally. 

Syntax: format(number, scientific = FALSE)

Returns : The exponential number with the exactly same number of digits. However, the output is returned as a character variable object. So, it needs to be converted to an integer explicitly, in order to use it for further purposes. 

Example 1:

R




# declaring an integer number
num = 1000000000000
print ("original number")
print (num)
  
print ("modified number")
format(num, scientific = F)

Output:

[1] "original number"
[1] 1e+12
[1] "modified number"
[1] "1000000000000"

Example 2:

R




# declaring an exponential number
num =2.21e+09
print ("original number")
print (num)
  
print ("modified number")
format(num, scientific = FALSE)

Output

[1] "original number"
[1] 2.21e+09
[1] "modified number"
[1] "2210000000"



My Personal Notes arrow_drop_up
Recommended Articles
Page :