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)
.
In R, using scientific notation 10^ rather than e+
To print the number you can treat it as a string and use sub
to reformat it:
changeSciNot <- function(n) {
output <- format(n, scientific = TRUE) #Transforms the number into scientific notation even if small
output <- sub("e", "*10^", output) #Replace e with 10^
output <- sub("\\+0?", "", output) #Remove + symbol and leading zeros on expoent, if > 1
output <- sub("-0?", "-", output) #Leaves - symbol but removes leading zeros on expoent, if < 1
output
}
Some examples:
> changeSciNot(5)
[1] "5*10^0"
> changeSciNot(-5)
[1] "-5*10^0"
> changeSciNot(1e10)
[1] "1*10^10"
> changeSciNot(1e-10)
[1] "1*10^-10"
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)
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"
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.
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"
using scientific notation in R
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
Related Topics
How to Reorder the Items in a Legend
Find Locations Within Certain Lat/Lon Distance in R
Generating Multiple Plots in Ggplot by Factor
Geom_Density to Match Geom_Histogram Binwitdh
Why Is Subsetting on a "Logical" Type Slower Than Subsetting on "Numeric" Type
Writing Data Frame to PDF Table
How to Return 5 Topmost Values from Vector in R
First Day of the Month from a Posixct Date Time Using Lubridate
Should I Avoid Programming Packages with Pipe Operators
How to Find Common Rows Between Two Dataframe in R
In R, How to Check If Two Variable Names Reference the Same Underlying Object
Count Number of Non-Na Values by Group
Filtering Rows in R Unexpectedly Removes Nas When Using Subset or Dplyr::Filter
Data Table - Select Value of Column by Name from Another Column
Sum Object in a Column Between an Interval Defined by Another Column