Print values on R console or file using cat() or print()

In this tutorial we look at functions to print values on R console or file.. The values are normally printed on the console since that is the default sink, but you can specify that the output be sent to a file by opening up a sink to the file. we will see an example of it in this tutorial.

Printing Strings using print() function
Lets create a sample string

> a <- "This is a sample string"

A reminder before we proceed : Strings in R imply a character vector. If you recall the lowest datastructure in R is a vector. So a String is a character vector of length 1. You can do this :

> a[1]
[1] "This is a sample string"

the
print
function can be used to print the argument. Print also returns the argument so it can be assigned.

> b < print(paste(a,1))
[1] "This is a sample string 1"
> b
[1] "This is a sample string 1"
> 

We will look at the paste function later on. The string function is generic and many packages define their own version. The default print method is known as print.default. It can accept some other arguments too. Lets look at some.Use quote=FALSE to turn of quotes for strings

> x <- "This is a sample string"
> print(x)
[1] "This is a sample string"
> print(x,quote=FALSE)
[1] This is a sample string

The “digits” argument specify the number of digits that should be displayed. Numbers are rounded off to that digits.

> x <- 10.4678
> print(x,digits=3)
[1] 10.5

NA in number can be printed specially. Here’s a vector that contains NA

> a=c(1,2,NA,4,NA,6,7)
> a
[1]  1  2 NA  4 NA  6  7
> print(a,na.print="-999")
[1]    1    2 -999    4 -999    6    7

In the example above we replaced all NA with “-999”. We could increase the gap between numbers (for aesthetics and readability)

> print(a,na.print="-999",print.gap=2)
[1]     1     2  -999     4  -999     6     7

Strings can be right aligned instead of the default left alignment

> a=c("one","two","three")
> print(a)
[1] "one"   "two"   "three"
> print(a,right=TRUE)
[1]   "one"   "two" "three"

Sometime we have a huge number and we want to limit the number of entries that we want to print. Use this “max” argument.

> a <- c(1:10000)
> print(a,max=10)
 [1]  1  2  3  4  5  6  7  8  9 10
 [ reached getOption("max.print") -- omitted 9990 entries ]

Printing a data frame.

The print function can be used to print a dataframe. It takes in most of the parameters as in default print method described above. It however takes in one more parameter called row.names. if set to TRUE the row names are printed

> df <- data.frame(a=c(1,2,3,4,5),b=c('a','b','c','d','e'),c=c(10L,20L,30L,40L,50L))
> print(df)
  a b  c
1 1 a 10
2 2 b 20
3 3 c 30
4 4 d 40
5 5 e 50
> print(df,row.names=F)
 a b  c
 1 a 10
 2 b 20
 3 c 30
 4 d 40
 5 e 50

Printing to a file.

R can also send the output to a file instead of the console. Here’s how to do it

> sink("output.txt")
> print(df)
> sink()

The first line opens up a connection to the output.txt file. The second line prints the data frame df (on the file connection) and the last line closes the file connection.

Printing Strings using cat() function

The cat function can be used to concatenate strings and then output them. Lets see some examples

> cat("a","b","c")
a b c> 

The 3 characters are combined in a string and separated by a space. Note that newline is not added

> cat("a","b","c",fill=TRUE)
a b c
> 

A new line is now added. You can also explicitly add a new line

> cat("a","b","c","\n")
a b c 

Note that instead of fill=TRUE you can also specify a width after which a new line is inserted

> cat("a","b","c",fill=2)
a 
b 
c

This inserts a new line after every two chracters. Now, what if you want to separate the arguments to cat by another separator?

Easy, use sep=

> cat("a","b","c",sep=",",fill=TRUE)
a,b,c

Note that cat works only with atomic vectors

This covers the basics of printing. However, this can be quite powerful since it allows you create text files in R with almost any kind of data. In a subsequent tutorial we will look at the
format()
function to format R objects for pretty printing.

Leave a Reply

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