Warning: session_start(): open(/tmp/sess_5a64b65e1077f8f80161e782354b5707, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
print() Function In R Programming Language - LearnShareIT

print() Function In R Programming Language

print() function in r

Each programming language has its way of printing data to the console, and R is no exception. This article will help you use the print() function to print data to the console.

What is the print() function in R?

If you want to print data to the console in R, you can use the print() function. This function has three parameters and does not return another value.

To better understand how to use this function, see below syntax and example.

Syntax:

If you want to use the print() function, you need to know the syntax of this function. So, see the syntax below:

print(data, digits, na.print)

Parameters:

  • data: The data which print out to the console.
  • digits: It establishes the bare minimum of significant digits.
  • na.print: It defines the output format for NA values.

Print without digits and na. print

Here, we can use the print() function in R to print out data to the console. The data can be a vector, data frame, string, matrix, …

Let’s follow the code below:

# Print a string without a variable
print("Learn Share IT 1")

# Initial a string
s = "Learn Share IT 2"

# Print a string and pass a variable
print(s)

Output

[1] "Learn Share IT 1"
[1] "Learn Share IT 2"

Similar to a matrix, vector, or data frame:

# Print vector 
data <- 5:20
print("Print data is: ")
print(data)

# Print data frame
df <- data.frame(
    math = c(5,6,7),
    english = c(8,7,10)
)

print("Print data frame is: ")
print(df)

Output

[1] "Print data is: "
[1]  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
[1] "Print data frame is: "
  math english
1    5       8
2    6       7
3    7      10

Print with digits argument

Using the print() function’s digits argument, we can print the floating-point numbers.

# Initial a variable
data <- 1 / 3

# Using print() function to print data
print("Print data with floating point = 4: ")
print(data, digits = 4)

print("Print data with floating point = 10: ")
print(data, digits = 10)

Output

[1] "Print data with floating point = 4: "
[1] 0.3333
[1] "Print data with floating point = 4: "
[1] 0.3333333333

Print with na.print

We will replace NA values with zero in the output.

data <- matrix(c(12, NA, 16, 51, NA, 20),
    byrow = TRUE,
    nrow = 3,)
    
# View data
print(data)

print("Print data after replace is: ")
print(data, na.print = "0")

Output

[1] "Data is:"
    [,1] [,2]
[1,]   12   NA
[2,]   16   51
[3,]   NA   20
[1] "Data after replace is: "
    [,1] [,2]
[1,]   12    0
[2,]   16   51
[3,]    0   20

Print with paste function

To print output including a string and a variable combined, R has the paste() method. The print() function has this method.

# Initial a variable
s <- "Learn Share IT"

# Using paste() function inside print()
print(paste(s, "is the best"))

Output

[1] "Learn Share IT is the best"

Summary

The above is all I can share with you about the print() function in R, and I hope you find the best way for you. If you have any questions, please leave a comment below, and I will answer your questions. Have a good day!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *