Home » R » Print Methods in R

Print Methods in R

In this article, we will study the usage of different data structures and the applications of print methods in the R programming language. 

The print() method in R is used to display the output over the console. The print() method can be used with all kinds of data types. The method is available in base R and is used to display both values as well as the variables used in the code. The method doesn’t return any values. 

The syntax of the print method is as follows: 

print (var , na.print , digit )
  • Where, var is the variable to be displayed:
  • na.print - by default, the value is TRUE. In case any other string is specified, the NA values are replaced with the specified values. 
  • digit - indicative of the minimal number of significant digits.

Usage of print with singular variables

The variables can be simply passed as an argument to the print() method. The value associated with that variable will be displayed on the console. Below is an example of print method in R:

#declaring variable 
var = 10-2i
#printing value
print("Printing a string value")
#printing variable
The code produces the following output : 
[1] "Printing a string value"
[1] 10-2i

Usage of the print method with for loop in R

The values in vectors can be displayed using a print statement enclosed within a loop. The loop along with the print statement can be used to print the data belonging to n-dimensional objects. 

#declaring a vector 
vec <- c(3+4i,2-i,1+i,-i)

#printing the vector elements
for( i in vec){
The output produced by the code is : 
[1] 3+4i
[1] -3+0i
[1] 6+0i
[1] -5+0i

Usage of print method with floating-point notation in R

The digits parameter can be used to regulate the number of digits to be shown in the number. 

#declaring a floating number
num = 9.4393295
#printing 3 digits of number
print(num , digits = 3)
#printing 1 digit of number
print(num , digits = 1)
The code produces the following output : 
[1] 9.44
[1] 9

Usage of print method with dataframes R

Dataframe is a tabular structure composed of rows and columns. The entire data structure can be displayed in an organized way using the print() method in R.

#declaring dataframe
data_frame <- data.frame(col1 = c(NA,2,3),
                         col2 = c("R","python",NA),
                         col3 = c(TRUE,TRUE,FALSE))
#printing the data frame
print("Data frame with NAs")
The code produces the following output : 
[1] "Data frame with NAs"
  col1   col2  col3
1   NA      R  TRUE
2    2 python  TRUE
3    3   <NA> FALSE

Usage of print method with matrices in R

The data in the matrices is atomic, but it may contain missing values denoted by NA. The NA values are ambiguous and mostly are removed while performing the data transformation. The following code snippet illustrates the procedure for the replacement of NA values in a matrix using print method in R. 

#declaring matrix 
mat = matrix(c(1,3,NA,NA,4,6,2,NA,10,NA), nrow = 5)

#printing mtrix 
print("Matrix with NA")
#printing mtrix 
print("Matrix without NA")
#replacing the NA value with -1
print(mat, na.print = "")
Output produced : 
[1] "Matrix with NA"
     [,1] [,2]
[1,]    1    6
[2,]    3    2
[3,]   NA   NA
[4,]   NA   10
[5,]    4   NA
[1] "Matrix without NA"
     [,1] [,2]
[1,]    1    6
[2,]    3    2
[4,]        10
[5,]    4