How To Use The cat() Function In R

You may be already familiar with print() – a common solution for displaying text. But there is also the cat() function in R, which works in a slightly different way. Here we will show what you can do with this function.

Table of Contents

cat() Function In R

The cat() function can print one or more arguments to the R console. When needed, it will coerce them into characters before printing. This is useful for printing messages or debugging your code.

The print() function is a popular choice for these tasks. But unlike it, cat() does not add a new line after printing each argument. This means that when passed multiple objects, can() will print them on the same line, separated by a space.

Here is an example of how you might use the cat() function in R:

a <- 'Welcome'
b <- 'to'
c <- 'LearnShareIT'
cat(a, b, c)

Output

Welcome to LearnShareIT

In this example, we assign three words to three variables. Finally, we pass them to the cat() function, telling it to print those words in the R console. The final output consists of those three words and looks like a message, thanks to the spaces between them.

The cat() function accepts variables of other types too. It just coerces them into character strings before concatenating and printing them.

Here is how this function prints numeric values:

x <- 45
y <- 3
z <- -23
cat(x, y, z)

Output

45 3 -23

There are also several arguments you can use to control how cat() prints R objects. By default, it uses a space to separate objects. But you can set the sep argument to a different character string to change this separator. For example, you can set it to a hyphen:

cat(a, b, c, sep = "-")
Welcome-to-LearnShareIT

In the default mode, cat() prints to the standard output. But it actually can send the output to a file on your filesystem as well. Set the file argument to the name of the file you want cat() to print to:

cat(a, b, c, file = "output.txt")

This changes the behavior of cat(). The output will no longer be displayed on the R console. Instead, open the output.txt in the current working directory, and you will see it.

Be careful when you use this option, as it can overwrite the existing contents of your files. If you want cat() to append its output to an existing file, set append to TRUE. This way, you won’t lose the previous contents in the file.

cat(a, b, c, file = "output.txt", append = TRUE)

If you want to manually break into a new line, use the escape character \n.

cat(a, b, "\n", c)
Welcome to 
LearnShareIT

The default behavior of cat() is that it only creates new lines when they are explicitly created like this. You can also tell it to do so when a line is wider than the option width or a certain value you provide with the fill argument.

In this example, cat() automatically generates a new line feed because the line exceeds the fill value.

 cat(a, b, c, fill = 15)
Welcome to 
LearnShareIT

Summary

The cat() function in R can print multiple arguments to the same line in the console. It coerces the arguments first, if necessary, then concatenates and prints them. Use it when you want to quickly combine multiple values in R.

Posted in R

Leave a Reply

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