The cat() function concatenates and prints objects to the console or file. It’s commonly used for creating formatted output, printing formatted messages, and writing text to files. Unlike print(), which adds extra formatting, cat() gives you complete control over what’s displayed.

I use cat() all the time when I need clean, formatted output without R’s default brackets and quotes.

When to Use cat()

You’ll use cat() when you need:

  • Formatted console output without automatic brackets
  • Combining multiple values with custom separators
  • Writing formatted text to files
  • Creating progress messages in functions
  • Building strings with specific formatting

Basic Syntax

cat(..., file = "", sep = " ", append = FALSE)
  • : Values to concatenate
  • file: Output destination ("" = console, filename = file)
  • sep: Separator between values
  • append: TRUE = add to file, FALSE = overwrite

Examples with Explanations

Use cat() Function to Concate Object

Let’s see example how we can concat strings using cat() function.

# Concat objects
cat("This","is","R","language")

Output:

This is R language

The output shows strings are concated using cat() function.

Use cat() Function to Concate Object with Separator

You can concate objects using cat() function with separator:

# Concat strings and seperated by ,
cat("This","is","R","language",sep=",")

Output:

This,is,R,language

Here the output shows objects are concated using cat() function and separated by ,.

Let’s see another example how we can concate object with \n separator.

# Concat strings and seperated by \n
cat("This","is","R","language",sep="\n")

Output:

This
is
R
language

The output shows strings are concated and each string printed on next line.

Use cat() Function to Concate Object with Separator and Store in File

Let’s see how we can concate object and store in file:

# Concat strings which seperated by \n and store output in file
cat("This","is","R","language",sep="\n", file = "data.txt")

Here in the above code we pass strings which are separated by “\n” to cat() function. When storing strings in a file, each string is stored on a new line.

Common Mistakes to Avoid

Mistake 1: Confusing cat() with print()

# ❌ print() adds brackets and quotes
print("Hello")
# [1] "Hello"

# ✅ cat() displays cleanly (no brackets or quotes)
cat("Hello")
# Hello

Mistake 2: Forgetting sep parameter creates default spacing

# ❌ Default separator is space - can look crowded
cat("Name:", "John", "Age:", 25)
# Name: John Age: 25

# ✅ Use sep parameter for better formatting
cat("Name:", "John", "Age:", 25, sep = "\n")
# Name:
# John
# Age:
# 25

Mistake 3: Not using append when adding to existing file

# ❌ WRONG - Overwrites file each time
cat("Line 1\n", file = "output.txt")
cat("Line 2\n", file = "output.txt")  # File now only contains "Line 2"

# ✅ CORRECT - Use append = TRUE to add to file
cat("Line 1\n", file = "output.txt", append = FALSE)  # Create file
cat("Line 2\n", file = "output.txt", append = TRUE)   # Add to file

Mistake 4: Forgetting \n for newlines

# ❌ WRONG - Everything on one line
cat("Line 1", "Line 2", "Line 3")
# Line 1 Line 2 Line 3

# ✅ CORRECT - Use \n for newlines
cat("Line 1\n", "Line 2\n", "Line 3\n")
# Line 1
# Line 2
# Line 3

Pro Tips

  1. cat() vs paste() - cat() prints directly; paste() returns a string
  2. Use \t for tabs - cat("Name\tAge\n") for table formatting
  3. cat() returns NULL - Don’t try to assign its output
  4. Combine with sprintf() - For formatted numbers: cat(sprintf("Value: %.2f\n", 3.14159))

See Also