What is print() Function in R

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.

Syntax

print(data, digits, na.print)

Parameters

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)

Output

[1] 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)

Output

[1] 0.2
[1] 4.83
[1] 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 = "")

Output

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

Output

[1] "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)"))

Output

[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.

Conclusion

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.

Related posts

sprintf in R

encodeString in R

Leave a Comment