The cat() function in R – A Quick Introduction

Filed Under: R Programming
R Cat() Function

Hello, readers! In this article, we will be focusing on the cat() function in R.

So, let us begin!! 馃檪


Working of the cat() function in R

R provides us with various functions to deal with the data and present the strings after manipulations as a consolidated result.

One such function is cat() function in R programming.

With cat() function, we can displayed manipulated strings as output. That is, it is used to print the data in a customized manner on the screen.

Have a look at the below syntax!

cat(data, sep, fill)
  • data: The piece of information that needs to be printed on the screen.
  • sep: The portion of string that we want to concatenate to the data.
  • fill: If set to TRUE, it adds a new line at the end of the data.

Thus, with cat() function, we can customize what needs to be shown on the screen. The various parameters present can help us add or align the string or data that has to be displayed on the screen.

Let us focus on the implementation of the same in the upcoming section.


Example 01: Using the cat() function in R

In the below example, we have displayed a string passed to the cat() function. Along with it, we have attached the value of the variable ‘data’ post manipulation to the string.

That is, as soon as the compiler encounters the cat() function, it first evaluates the variable’s value to 7 and then concatenates it to the statically passed string as shown below–

Example:

rm(list = ls())
data = 0
cat("The number of days in a week = ", data <- data + 7, "\n")

Output:

The number of days in a week =  7 

Example 02: R cat() function with ‘sep’ argument

In order to add certain string or argument to the data passed to cat() function, sep argument can be used. The value set to the parameter ‘sep’ gets printed after the data.

In the below example, we have data to start from 10 and end at 20. Passing sep = ‘:’ would now print the value of sep after every number in the variable ‘data’.

Example:

rm(list = ls())
data = 10:20
cat(data, sep = ":")
cat("\n")

Output:

To notice, we have added ‘\n’ through the cat() function, that is the reason it does not print ‘:’ after the value 20.

10:11:12:13:14:15:16:17:18:19:20

Example 03: R cat() function with ‘fill’ argument

R cat() function provides us with ‘fill‘ argument that allows us to add a new line at the end of the string passed to the cat() function.

By setting fill to True, it adds a new line after the string printed.

Example:

rm(list = ls())
data = 10:20
cat(data, sep = ":",fill = TRUE)
print("Task is done")

Output:

As seen below, a new line is added after the string and then the output of the print() statement is displayed.

10:11:12:13:14:15:16:17:18:19:20

[1] "Task is done"

Conclusion

By this, we have come to the end of this topic. Do let us know in the comment box, if you come across any question.

For more such posts related to R programming, Stay tuned with us!

Till then, Happy Learning! 馃檪

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content