When working with R in an interactive mode, you don’t need to use any functions or methods to print the result of your expressions; it will print them automatically.
Simply typing a variable or expression will print its value.
data <- 42
data
# [1] 42
However, you need functions to see the output if you are working in a script where you have written a program.
Here are six ways you can use to print the output of a program:
The print() function is explicitly used to display the value of an object. It is a standard method for printing output to the console.
data <- 1:5
print(data)
# [1] 1 2 3 4 5
data <- 1 / 5
data2 <- 111 / 23
data3 <- 10900 / 2966
print(data, digits = 2)
print(data2, digits = 3)
print(data3, digits = 4)
# [1] 0.2
# [1] 4.83
# [1] 3.675
If you pass the na.print argument to empty, it will replace NA values with nothing in the output.
mat <- matrix(c(1, NA, 3, 4, NA, 6, 7, 8, NA),
nrow = 3, byrow = TRUE
)
print(mat)
cat("After passing na.print argument to empty", "\n")
print(mat, na.print = "")
The cat() function concatenates and prints its arguments. It helps print custom messages or formatting output. It does not add quotes. For a cleaner console output, you can use this approach.
x <- 21
cat("The value of x is:", x, "\n")
# The value of x is: 21
The cat() method does not automatically add a new line, as you can see from the above code. We added a newline character “\n” manually.
It cannot handle complex objects like a matrix.
mat <- matrix(1:4, nrow = 2)
cat(mat)
# 1 2 3 4%
You can see that the console output is wrong, and it does not look like a matrix.
The message() prints a message but does not stop the execution of a function.
The warning() issues a warning message but, like message(), allows the execution to continue.
The stop() prints an error message and stops the execution.
message("This is a message.")
warning("This is a warning.")
Output
This is a message.
Warning message:
This is a warning.
The sprintf() function formats strings (similar to printf() in C/C++). It’s helpful when you need to format numbers or strings before printing.
x <- 19
sprintf("The value of x is: %f", x)
Output
[1] "The value of x is: 19.000000"
The paste() function inserts spaces by default. The paste0() function concatenates without spaces.
paste("AI", "Learning")
# "AI Learning"
paste0("Hello", "world")
# "AILearning"
To print output to a file, you can use cat(), write.table(), or similar functions with a file connection.
x <- 1921
cat("The value of x is:", x, file = "output.txt", append = TRUE)
Output
That’s it!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Set the current working directory The setwd() function sets the working directory to the new…
The sd() function in R calculates the sample standard deviation of a numeric vector or…
The dnorm() function in R calculates the value of the probability density function (pdf) of…
R rep() is a generic function that replicates elements of vectors and lists for a…
The strsplit() function in R splits elements of a character vector into a list of…
The rnorm() method in R generates random numbers from a normal (Gaussian) distribution, which is…