Each programming language has its way of printing data to the console, and R is no exception. This article will help you use the print() function to print data to the console.
What is the print() function in R?
If you want to print data to the console in R, you can use the print() function. This function has three parameters and does not return another value.
To better understand how to use this function, see below syntax and example.
Syntax:
If you want to use the print() function, you need to know the syntax of this function. So, see the syntax below:
print(data, digits, na.print)
Parameters:
- data: The data which print out to the console.
- digits: It establishes the bare minimum of significant digits.
- na.print: It defines the output format for NA values.
Print without digits and na. print
Here, we can use the print() function in R to print out data to the console. The data can be a vector, data frame, string, matrix, …
Let’s follow the code below:
# Print a string without a variable print("Learn Share IT 1") # Initial a string s = "Learn Share IT 2" # Print a string and pass a variable print(s)
Output
[1] "Learn Share IT 1"
[1] "Learn Share IT 2"
Similar to a matrix, vector, or data frame:
# Print vector data <- 5:20 print("Print data is: ") print(data) # Print data frame df <- data.frame( math = c(5,6,7), english = c(8,7,10) ) print("Print data frame is: ") print(df)
Output
[1] "Print data is: "
[1] 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[1] "Print data frame is: "
math english
1 5 8
2 6 7
3 7 10
Print with digits argument
Using the print() function’s digits argument, we can print the floating-point numbers.
# Initial a variable data <- 1 / 3 # Using print() function to print data print("Print data with floating point = 4: ") print(data, digits = 4) print("Print data with floating point = 10: ") print(data, digits = 10)
Output
[1] "Print data with floating point = 4: "
[1] 0.3333
[1] "Print data with floating point = 4: "
[1] 0.3333333333
Print with na.print
We will replace NA values with zero in the output.
data <- matrix(c(12, NA, 16, 51, NA, 20), byrow = TRUE, nrow = 3,) # View data print(data) print("Print data after replace is: ") print(data, na.print = "0")
Output
[1] "Data is:"
[,1] [,2]
[1,] 12 NA
[2,] 16 51
[3,] NA 20
[1] "Data after replace is: "
[,1] [,2]
[1,] 12 0
[2,] 16 51
[3,] 0 20
Print with paste function
To print output including a string and a variable combined, R has the paste() method. The print() function has this method.
# Initial a variable s <- "Learn Share IT" # Using paste() function inside print() print(paste(s, "is the best"))
Output
[1] "Learn Share IT is the best"
Summary
The above is all I can share with you about the print() function in R, and I hope you find the best way for you. If you have any questions, please leave a comment below, and I will answer your questions. Have a good day!
Maybe you are interested:
- length() function in R: How To Use The Length Function In R
- Head function in R: How to use head() in R
- pt() function in R: How to use the pt function in R

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript