Session Ready
Exercise

Using format() with numbers

The behavior of format() can be pretty confusing, so you'll spend most of this exercise exploring how it works.

Recall from the video, the scientific argument to format() controls whether the numbers are displayed in fixed (scientific = FALSE) or scientific (scientific = TRUE) format.

When the representation is scientific, the digits argument is the number of digits before the exponent. When the representation is fixed, digits controls the significant digits used for the smallest (in magnitude) number. Each other number will be formatted to match the number of decimal places in the smallest number. This means the number of decimal places you get in your output depends on all the values you are formatting!

For example, if the smallest number is 0.0011, and digits = 1, then 0.0011 requires 3 places after the decimal to represent it to 1 significant digit, 0.001. Every other number will be formatted to 3 places after the decimal point.

So, how many decimal places will you get if 1.0011 is the smallest number? You'll find out in this exercise.

Instructions
100 XP
  • Format c(0.0011, 0.011, 1) with digits = 1. This is like the example described above.
  • Now, format c(1.0011, 2.011, 1) with digits = 1. Try to predict what you might get before you try it.
  • Format percent_change by choosing the digits argument so that the values are presented with one place after the decimal point.
  • Format income by choosing the digits argument so that the values are presented as whole numbers (i.e. no places after the decimal point).
  • Format p_values using a fixed representation.