 How to Format Numbers in R - dummies

When you produce reports in R, you will want your numbers to appear all nicely formatted to enhance the impact of your data on the viewer. You can use format() to turn your numbers into pretty text, ready for printing. This function takes a number of arguments to control the format of your result. Here are a few:

• trim: A logical value. If FALSE, it adds spaces to right-justify the result. If TRUE, it suppresses the leading spaces.

• digits: How many significant digits of numeric values to show.

• nsmall: The minimum number of digits after the decimal point.

In addition, you can control the format of the decimal point with decimal.mark, the mark between intervals before the decimal point with big.mark, as well as the mark between intervals after the decimal point with small.mark.

For example, you can print the number 12345.6789 with a comma as decimal point, spaces as the big mark, and dots as the small mark:

```> format(12345.6789, digits=9, decimal.mark=",",
+   big.mark=" ",small.mark=".", , small.interval=3)
 "12 345,678.9"```

As a more practical example, to calculate the means of some columns in mtcars and then print the results with two digits after the decimal point, use the following:

```> x <- colMeans(mtcars[, 1:4])
> format(x, digits=2, nsmall=2)
mpg   cyl   disp    hp
" 20.09" " 6.19" "230.72" "146.69"```

Notice that the result is no longer a number but a text string.

If you’re familiar with programming in languages similar to C or C++, then you also may find the sprintf() function useful, because sprintf() is a wrapper around the C printf() function. This wrapper allows you to paste your formatted number directly into a string.

Here’s an example of converting numbers into percentages:

```> x <- seq(0.5, 0.55, 0.01)
> sprintf("%.1f %%", 100*x)
 "50.0 %" "51.0 %" "52.0 %" "53.0 %" "54.0 %" "55.0 %"```

This is what it does: The first argument to sprintf() indicates the format — in this case, “%.1f %%”. The format argument uses special literals that indicate that the function should replace this literal with a variable and apply some formatting. The literals always start with the % symbol. So, in this case, %.1f means to format the first supplied value as a fixed point value with one digit after the decimal point, and %% is a literal that means print a %.

To format some numbers as currency — in this case, U.S. dollars — use the following:

```> set.seed(1)
> x <- 1000*runif(5)
> sprintf("\$ %3.2f", x)
 "\$ 265.51" "\$ 372.12" "\$ 572.85" "\$ 908.21" "\$ 201.68"```

As you saw earlier, the literal %3.2f means to format the value as a fixed point value with three digits before the decimal and two digits after the decimal.

The sprintf() function is a lot more powerful than that: It gives you an alternative way of pasting the value of any variable into a string:

```> stuff <- c("bread", "cookies")
> price <- c(2.1, 4)
> sprintf("%s costed \$ %3.2f ", stuff, price)