print in R: How to Print Variables using print() Function

Every language provides some functions that can help you print the data on the console, and R is no different. To print the data on the console in R, use the print() function.

print in R

The print() is a built-in R function that prints its argument. The print() function accepts three arguments and does not return any value. Instead, the print() method will print out the argument on the screen.


print(data, digits, na.print)


data: It specified the argument to be displayed.

digits: It defines the minimal number of significant digits.

na.print: It indicates NA values output format.


Create a vector using the colon(:) operator.

data <- 1:5


[1] 1 2 3 4 5

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)


[1] 0.2
[1] 4.83
[1] 3.675

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)

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 that after passing the na.print to empty, some values are entirely disappeared from the matrix because we are telling R that replace NA values with nothing.

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"))


[1] "Covid-19 is surging in India"

Printing output using paste() inside print() function in R

The paste() is a built-in R function that prints output with string and variable together. The paste() method converts its arguments to character strings. There is also one more function related to paste() function called 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 of 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)"))


[1] "Aloha Mora The paste() function inside print() method)"
[1] "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.

That is it for print() function in R.

Related posts

sprintf in R

encodeString in R

Leave a Comment