cat() in R: How to Concatenate Objects in R

R cat() method converts its arguments to character strings, concatenates them, separating them by the given sep= string, and then prints them.

cat() in R

The cat() is a built-in R function that concatenates and prints the objects. It takes up to six parameters and outputs the objects, concatenating the representations.

Syntax

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

Parameters

The … are R objects.

The file is a connection or a character string naming the file to print to. If ” “ (the default), cat prints to the standard output connection.

The sep is a character vector of strings to append after each element.

The fill is a logical or (positive) numeric, controlling how the output is broken into successive lines.

The labels are a character vector of labels for the lines printed. Ignored if the fill is FALSE.

The append is a logical value. Only used if the argument file is the file’s name (and not a connection or “|cmd”).

Return Value

It returns None (invisible NULL).

Example

The simple cat() function will convert the values into character values, then combine those values and print them.

cat("Elon", "Musk", "Buys", 1.5, "Billion", "of Bitcoin")

Output

Elon Musk Buys 1.5 Billion of Bitcoin

Let’s see another simple example for cat function with only two arguments.

cat(18:21, sep = "\n")

Output

18
19
20
21

We passed “\n” as a separator to the cat() function.

Passing an atomic vector to the cat() function

If you pass it an atomic vector, it prints each element of the input separated by spaces. So let’s pass LETTERS to the cat() function and see the output.

cat(LETTERS)

Output

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

cat() function returns NULL

The cat() method returns NULL; it exists purely for the side-effect of writing output, not creating new variables.

data <- cat(18:21, "\n")

is.null(data)

Output

18 19 20 21
[1] TRUE

Passing Matrix to the cat() function

If you pass a matrix to the cat(), it will ignore the dimensions and pretend that you passed a vector.

mtrx <- matrix(1:9, 3)
mtrx
cat(mtrx)

Output

    [,1] [,2] [,3]
[1,]  1    4    7
[2,]  2    5    8
[3,]  3    6    9

1 2 3 4 5 6 7 8 9

You can see that it ignores the dimension and returns the output as a vector.

Passing List to the cat() function

If you pass a list to the cat() function, it will throw an error.

lst <- list(1, 2, 3, 4)
cat(lst)

Output

Error in cat(lst) : argument 1 (type 'list') cannot be handled by 'cat'
Execution halted

You can handle the exception by using the tryCatch() function.

cat() function with Filename and Append=False

Let’s see an example that concatenates the values and stores the value in the csv file. By default Append = False.

cat(18:21, file="data.csv", sep = "\n", append ="FALSE")

It will create a CSV file with 18 to 21 values. Each value will be in a new line since we have passed sep = “\n”.

Output

18
19
20
21

The output will be a data.csv file with the above values.

The append = FALSE means if you rerun the above code, it will override the existing content of the data.csv file.

cat() function with Filename and Append=TRUE

If you pass append = TRUE, it will append the text to the existing content to the file and not override it.

cat(18:21, file="data.csv", sep = "\n", append ="TRUE")

Output

18
19
20
21
18
19
20
21

You can see from the output that it appends another four elements to the data.csv file.

Difference between cat() and print() in R

The cat() method can be considered a low-level version of the print() function, but these functions are different in many ways.

The cat() function is valid only for atomic types (logical, integer, real, complex, character) and names. It means you cannot call a cat() function on a non-empty list or any object.

In practice, it simply converts arguments to characters and concatenates so you can think of something like as.character() %>% paste() function.

The print() is an inbuilt generic R function that can define a specific implementation for a certain S3 class.

The print() function returns its arguments whereas the cat() function returns invisibly returns NULL.

That is it for this tutorial.

See also

R print()

R substr()

R strsplit()

Leave a Comment