3 Formatting Text and Numbers

3.1 Introduction

A common task when working with character strings involves printing and displaying them on the screen or on a file. In this chapter you will learn about the different functions and options in R to print strings in a wide variety of common—and not so common—formats.

3.2 Printing and formatting

R provides a series of functions for printing strings. Some of the printing functions are useful when creating print methods for programmed objects’ classes. Other functions are useful for printing output either in the R console or to a given file. In this chapter we will describe the following print-related functions:

Function Description
print() generic printing
noquote() print with no quotes
cat() concatenation
format() special formats
toString() convert to string
sprintf() C-style printing

3.3 Generic printing

The workhorse printing function in R is print(). As its names indicates, this function prints its argument on the R console:

To be more precise, print() is a generic function, which means that you should use this function when creating printing methods for programmed classes.

As you can see from the previous example, print() displays text in quoted form by default. If you want to print character strings with no quotes you can set the argument quote = FALSE

3.3.1 When to use print()?

When you type the name of an obbject in the R console, R calls the corresponding print method associated to the class of the object. If the object is a "data.frame", then R will dispatch the method print.data.frame and display the output on screen accordingly.

Most of the times you don’t really need to invoke print(). Usually, simply typing the name of the object will suffice. So when do you actually call print()? You use print() when your code is inside an R expression (i.e. code inside curly braces { }) and you want to see the results of one or more computational steps. Typical examples that require an explicit call to print() is when you are interested in looking at some value within a loop, or a conditional structure.

Consider the following dummy for loop. It iterates five times, each time adding 1 to the value of the iterator i:

The above code works and R executes the additions, but nothing is displayed on the console. This is because the command i + 1 forms part of an R expression, that is, it is within the braces { }. To be able to see the actual computations you should call print() like so:

3.4 Concatenate and print with cat()

Another very useful function is cat() which allows you to concatenate objects and print them either on screen or to a file. Its usage has the following structure:

cat(..., file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE)

The argument ... implies that cat() accepts several types of R objects (typically vectors). However, when we pass numeric and/or complex vectors, they are automatically converted to character strings by cat(). By default, the strings are concatenated with a space character as separator. This can be modified with the sep argument.

If you use cat() with only one single string, you get a similar (although not identical) result as noquote():

As you can see, cat() prints its arguments without quotes. In essence, cat() simply displays its content (on screen or in a file). Compared to noquote(), cat() does not print the numeric line indicator ([1] in this case).

The usefulness of cat() is when we have two or more strings that we want to concatenate:

You can use the argument sep to indicate a chacracter vector that will be included to separate the concatenated elements:

When we pass vectors to cat(), each of the elements are treated as though they were separate arguments:

The argument fill allows us to break long strings; this is achieved when we specify the string width with an integer number:

Last but not least, we can specify a file output in cat(). For instance, let’s suppose that we want to save the output in the file output.txt located in our working directory. This is done by specifying the file argument:

3.5 Encoding strings with format()

The function format() allows you to format an R object for pretty printing. Essentially, format() treats the elements of a vector as character strings using a common format. This is especially useful when printing numbers and quantities under different formats.

Some useful arguments used in format():

  • width the (minimum) width of strings produced
  • trim if set to TRUE there is no padding with spaces
  • justify controls how padding takes place for strings. Takes the values "left", "right", "centre", "none"

For controling the printing of numbers, use these arguments:

  • digits The number of digits to the right of the decimal place.
  • scientific use TRUE for scientific notation, FALSE for standard notation

By default, format() pads the strings with spaces so that they all have the same length.

For printing large quantities with a sequenced format we can use the arguments big.mark or big.interval. For instance, here is how we can print a number with sequences separated by a comma ","

3.6 Exercises

  1. Why do we say that print() is not really one function but a family of functions?

  2. Mention three differences between print() and cat().

  3. What happens when you pass a matrix to cat()? For instance:

  1. What happens when you pass a data frame object to cat()? For instance: