R Print Output

🖨️ R Print Output

In R, output means displaying results on the screen. There are multiple ways to print output depending on what you need.

🔹 1. Using print() Function

The most common way to display output.

print("Hello, R")

Output:

[1] "Hello, R"

✔ Automatically shows the value with index


🔹 2. Printing a Variable

x <- 25
print(x)

Or simply type the variable name:

x

Output:

[1] 25

🔹 3. Using cat() Function

cat() is used to print clean output without quotes or index.

cat("Welcome to R")

Output:

Welcome to R

✔ Best for user-friendly messages


🔹 4. Printing Multiple Values

Using print()

print(c(10, 20, 30))

Using cat()

cat("Values are:", 10, 20, 30)

Output:

Values are: 10 20 30

🔹 5. New Line in Output

cat("Hello\nWorld")

Output:

Hello
World

🔹 6. Printing Calculations

a <- 10
b <- 5
cat("Sum =", a + b)

Output:

Sum = 15

🔹 7. Printing Data Structures

Vector

v <- c(1, 2, 3)
print(v)

Data Frame

df <- data.frame(Name=c("A", "B"), Age=c(20, 25))
print(df)

🔹 8. Difference Between print() and cat()

Feature print() cat()
Shows index Yes No
Shows quotes Yes No
New line default Yes No
Best for Debugging User output

🔹 Best Practice ✔

  • Use print() for checking values

  • Use cat() for formatted messages

You may also like...