print & cat Functions in R (3 Examples) | Return Data to RStudio Console

 

In this article, I’ll show how to use the print and cat functions in the R programming language.

The article consists of three examples for the application of print and cat. To be more specific, the content is structured as follows:

Let’s dive right in:

Definitions & Basic R Syntaxes of print & cat Functions

 

Definitions: Please find the definitions of the print & cat functions below.

  • The print R function returns a data object to the R (or RStudio) console.
  • The cat R function returns a character string in a readable format.

 

Basic R Syntaxes: Please find the basic R programming syntaxes of the print and cat functions below.

print(any_data_type)                          # Basic R syntax of print function
cat(any_string)                               # Basic R syntax of cat function

 

In the following, I’ll show three examples for the application of the print and cat commands and functions in R.

Example 1: print() vs. cat() Functions in R

Example 1 explains how to apply print and cat to a character string. First, we have to create an exemplifying character string in R:

my_string <- "This is \nan example string"    # Create example string

If we want to return this character string to the RStudio console, we can either use the print function…

print(my_string)                              # Apply print to character string
# [1] "This is \nan example string"

…or the cat function:

cat(my_string)                                # Apply cat to character string
# This is 
# an example string

Compare the outputs of the two functions. As you can see, print is simply returning the character string as data object. The cat function, in contrast, converts/removes some code-specific information (e.g. \n is converted into a newline) returns a readable version of our character string.

 

Example 2: Using print Function to Return Data Frames

The cat function is typically used to return character strings. The print function, however, is often also used for other data formats.

In this Example, I’ll explain how to apply the print function to a data frame object. First, we have to create an example data frame:

data <- data.frame(x1 = 1:5,                  # Create example data
                   x2 = 3)

Now, we can use the print function to return our data frame to the RStudio console:

print(data)                                   # Apply print to data frame
#   x1 x2
# 1  1  3
# 2  2  3
# 3  3  3
# 4  4  3
# 5  5  3

Note that RStudio invisibly uses the print function when you are just running a line of code that only consists of the name of your data object. In other words: we can just run “data” to return our data frame to the console:

data                                          # Print without print function
#   x1 x2
# 1  1  3
# 2  2  3
# 3  3  3
# 4  4  3
# 5  5  3

 

Example 3: Using print Function to Modify Console Output

As you have seen in Example 2, the print function must not be used to return a data object to the console. However, the print function provides additional arguments that can be specified within the print function.

In this Example, I’ll show how to modify the console output when printing numeric values. First, let’s create a numeric data object:

my_value <- 8235.675324                       # Create example value

Now, we could print this number without using the print function:

my_value                                      # Default printing
# 8235.675

As you can see, by default the R programming language returned seven digits. The print function can be used to change this:

print(my_value, digits = 5)                   # Changing number of digits
# 8235.7

The previous R code specified to print only five digits to the console.

 

Video & Further Resources

I have recently published a video on my YouTube channel, which shows the R codes of this tutorial. You can find the video below.

 

 

In addition, you could read the other articles of my website:

 

Summary: At this point you should have learned how to use print and cat in R programming. If you have any further questions, please tell me about it in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top