print() Function in R

The print() function allows you to print objects to the console. For example, you can use the print() function to print vectors, matrices, data frames, and lists. In addition, the print() function can be used to print the results of a computation. This guide will discuss using the print() in R.

Syntax

print(x)

Arguments

  • x = The object can be of any type, including vectors, matrices, data frames, and lists.

Example 1: Print Values using print() function

#printing values
print("R Programming Guide")

Output

[1] "R Programming Guide"

Example 2: Print Variables using print() function

#creating a variable
x <- 25
#printing variable
print(x)

Output

[1] 25

Example 3: Print a Vector using print() function

#creating a vector
x <- c(1:5)
#printing vector
print(x)

Output

[1] 1 2 3 4 5

Example 4: Print a List using print() function

#creating a list
x <- list('Bruce','Wayne',1.6,'Martial Arts')
#printing list
print(x)

Output

[[1]]
[1] "Bruce"

[[2]]
[1] "Wayne"

[[3]]
[1] 1.6

[[4]]
[1] "Martial Arts"

Example 5: Print a Data Frame using print() Function

#printing the preloaded iris data frame
print(iris)

Output

Print Data Frame
Printing data frame to the console.

Leave a Comment