Home ยป How to prevent scientific notation in R

How to prevent scientific notation in R

  • by
SCIENTIFIC NOTATION IN R
Want to do a random act of kindness? Share this post.

Scientific notation can be handy if you want to save digits. However, if you need to present your results to the board, there’s gonna be that one guy who will ask what the ‘e’-thingy stands for. Here’s how you can make sure that your results are not returned in scientific notation. To solving problems and beyond!

What is scientific notation?

This paragraph is only if you’ve skipped physics classes altogether. Scientific notation is a way of expressing numbers that are too big or too small to be conveniently written in the layman’s way, the decimal form. What happens is that the number gets expressed in ‘times ten raised to the power of…’. Here are some examples from Wikipedia:

In R, we use the E-notation. For example, one billion will be expressed as 1e+09. In your head, you can simply replace the e with 10 and anything after that is the power to which you raise it.

How to ensure decimal notation

When does R print in scientific notation? It’s not entirely clear to me, but it seems to be when the amount of characters in scientific notation are less than the amount of characters in the decimal notation plus the “integer penalty”.

The integer penalty is a number you can set manually. For example, 100 000 (6 characters) will be reduced to 1e+05 (5 characters), but not if you set a penalty of 1 or more in the following way(s):

format(x,scientific=1)
format(x,scientific=9)

It also works in the other direction. You can even get scientific notation for 1000, simply by reducing the integer penalty to a negative number:

format(1000,scientific=-2)

If you would like to format one number (as a character) without scientific notation whatsoever, you can do that by simply setting the scientific parameter to F.

format(x,scientific=F)

Another thing you can do is ensure that you always have a fixed amount of numbers after the decimal point while using scientific notation. It is not optimal in terms of characters, but it is possible. For example, the code below will return 1.00e+03.

sprintf("%.2e", 1000)

You can also get rid of scientific notation throughout your whole project by setting an immensely high integer penalty in the options of your environment.

options(scipen=999)

Great success!

Want to do a random act of kindness? Share this post.