1. Turning numbers into strings
A common use of strings is to report numerical results. You might, for example, want to report an estimate for the additional sales generated by each dollar spent on advertising.
2. Turning a number into a string
You've got the estimate stored in R, let's say one point three four zero one nine zero two nine one zero zero, but is that what you want in your report? No, you probably want something like $1-point-34. You might argue you can do this manually every time you need to, but doing this with code means its easy to automate, update and reuse in the future. You can always force a number to be a string with as-dot-character. But R won't do any formatting and will output to 15 significant digits, way more than you need in most cases. Instead I recommend using the functions format or formatC. They both give you control over how a number is represented as a string but take slightly different approaches. First let's do a quick review
3. Fixed and scientific formats
of fixed and scientific formats for numbers. In fixed format the decimal point is in a fixed place, between the tenths and ones digits. In scientific format, the decimal point can be anywhere, it is always displayed after the first digit, and then we use an exponent to describe its position. Take the distance six thousand, three hundred and seventy one kilometres. In fixed format
4. Fixed and scientific formats
it is simply six, three, seven, one. In scientific format we would write it
5. Fixed and scientific formats
as six point three seven one times ten to the power three. That is, you take six point three seven one and multiply it by one thousand. Alternatively, you can think about taking six point three seven one and moving the decimal point three places to the right.
6. Fixed and scientific formats
The advantage of scientific notation is that very large and very small numbers can be written quite concisely. For example,
7. Fixed and scientific formats
the mass of the sun in kilograms is just under two million million million million million kilograms. In fixed format, it's hard for people to read and understand this number.
8. Fixed and scientific formats
In scientific format, its easy to read the order of magnitude. In R,
9. Fixed and scientific formats
scientific formats use an abbreviation, by dropping the multiplication symbol and using an e to denote the ten. Which should you use? Unfortunately there isn't an easy answer, it's up to you to decide which would be most readable by your audience. What we can teach you, is how to obtain the format you want for any number. Both format and formatC
10. format() and formatC()
take a vector of numbers as input. They both also allow you to specify whether the numbers should be in fixed or scientific format. This is controlled by the scientific argument to format, which is set to TRUE for scientific format and FALSE for fixed format. In formatC
11. format() and formatC()
it's controlled by a format argument which can be f for fixed, e for scientific, and additionally g which will use the scientific format only if it saves space. The behavior of many of the other arguments depends on whether a fixed or scientific notation is requested,
12. Let's practice!
you'll see a few of the most important ones in the following exercises.