Printing out to the Screen or to a File in R Programming – cat() Function

cat() function in R Language is used to print out to the screen or to a file.

Syntax:
cat(…, file = “”, sep = ” “, fill = FALSE, labels = NULL, append = FALSE)

Parameters:
…: atomic vectors, names, NULL and objects with no output
file: the file in which printing will be done
sep: specified seperator
fill: If fill=TRUE, a new line will be printed, otherwise not
labels: specified labels

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# cat functiom
  
# Creating some string and print it
x <- "GeeksforGeeks\n"
y <- "Geeks\n"
  
# Calling cat() function
cat(x)
cat(y)
  
# Creating a sequence from 1 to 9
x <- 1:9
  
# Calling cat() function
cat(x, sep =" + ")
cat("\n")
cat(x, sep =" / ")

chevron_right


Output:

GeeksforGeeks
Geeks
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# cat functiom
  
# Creating a sequence from 1 to 9
x <- 1:9
  
# Calling cat() function 
  
# fill value TRUE will print
# a new line 
cat(x, sep =" + ", fill = TRUE)
cat(x, sep =" / ", fill = FALSE)
  
# Printing new line
cat("\n")
  
# Each number from 1 to 9 will be
# assigned with alphabets a to i
cat(x, fill = 2, labels = paste("(", letters[1:9], "):"))         

chevron_right


Output:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9
( a ): 1 
( b ): 2 
( c ): 3 
( d ): 4 
( e ): 5 
( f ): 6 
( g ): 7 
( h ): 8 
( i ): 9



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.