Skip to content
Home » How to prevent scientific notation in R

How to prevent scientific notation in R

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!

By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

12 thoughts on “How to prevent scientific notation in R”

Leave a Reply

Your email address will not be published. Required fields are marked *