The print() is a built-in R function that prints its argument. It accepts three arguments, prints the argument on the screen, and does not return any value.
print(data, digits, na.print)
data: It specifies the argument to be displayed.
digits: It defines the minimal number of significant digits.
na.print: It indicates NA values output format.
Example 1: Simple program of print() function
Create a vector using the colon(:) operator.
data <- 1:5 print(data)
 1 2 3 4 5
Example 2: Passing digits argument
The print() function accepts the digits argument, which will help us print the floating-point values.
data <- 1 / 5 data2 <- 111 / 23 data3 <- 10900 / 2966 print(data, digits = 2) print(data2, digits = 3) print(data3, digits = 4)
 0.2  4.83  3.675
Example 3: Passing na.print argument to print() function
If you pass the na.print argument to empty, it will replace NA values with nothing in the output.
data <- matrix(c(21, NA, 46, 11, NA, 19), nrow = 3, byrow = TRUE) print(data) cat("After passing na.print argument to empty", "\n") print(data, na.print = "")
[,1] [,2] [1,] 21 NA [2,] 46 11 [3,] NA 19 After passing na.print argument to empty [,1] [,2] [1,] 21 [2,] 46 11 [3,] 19
As you can see in the output, after passing the na.print to empty, some values entirely disappeared from the matrix because we are telling R to replace NA values with nothing.
Example 4: Printing output using paste() function inside print() function
To concate strings in R, use the paste() function. We will define the paste() method inside the print() method.
data <- "Covid-19 " print(paste(data, "is surging in India"))
 "Covid-19 is surging in India"
Example 5: Printing output using paste() inside print() function in R
The paste() is a built-in R function that prints output with string and variable. The paste() method converts its arguments to character strings. Another function related to the paste() function is called the paste0() function.
The main difference between the paste() and paste0() function is that the parameter sep by default for paste is ” “, and for past0, it is “”. This means it is the difference between blank space.
rv <- "Aloha Mora" # Using paste() function inside print() method print(paste(rv, "The paste() function inside print() method)")) # Using paste0() function inside print() method print(paste0(rv, "The paste0() function inside print() method)"))
 "Aloha Mora The paste() function inside print() method)"  "Aloha MoraThe paste0() function inside print() method)"
You can see that there is one space after the “Aloha Mora” string in the paste() function, and in the case of the paste0() function, there are no spaces. It is visible while using the print() function.
The print() is a built-in function that prints a wide range of R objects in the console, including vectors, matrices, data frames, and lists. It can be used It takes one or more arguments, which are the objects, to be displayed.
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.