1: Cat function in R
cat function in R will combine character values and print them to the screen or save them in a file directly. The cat function coerces its arguments to character values, then concatenates and displays them. This makes the function ideal for printing messages and warnings from inside of functions
Syntax for cat function in R:
cat(… , file = “”, sep = ” “, fill = FALSE, labels = NULL, append = FALSE)
Arguments
…
one or more R objects, to be concatenated together.sep
a character string to separate the terms.collapse
an optional character string to separate the results.
cat('one',2,'three',4,'five')
one 2 three 4 five
cat(1:10,sep="\t")
1 2 3 4 5 6 7 8 9 10
cat(1:10,sep="\n")
1 2 3 4 5 6 7 8 9 10
cat(1:10,file="num_series.csv",sep="\n",append ="FALSE")
cat(1:10,file="num_series.csv",sep=",",append ="FALSE")
cat(1:10,file="num_series.csv",sep="\n")
cat(11:20,file="num_series.csv",sep="\n",append ="TRUE")
2: Paste function in R
Paste function in R is used to concatenate Vectors by converting them into character. paste0 function in R simply concatenates the vector with space separator. lets see an example of paste() Function in R and Paste0() Function in R
Syntax for Paste Function in R
- paste (…, sep = ” “, collapse = NULL)
Arguments
…
one or more R objects, to be concatenated together.sep
a character string to separate the terms.collapse
an optional character string to separate the results.
paste('one',2,'three',4,'five')
paste('X',1:5,sep='')
- ‘X1’
- ‘X2’
- ‘X3’
- ‘X4’
- ‘X5’
paste(c('one','two','three','four'),collapse=' and ')
paste(c('X','Y'),1:5,sep='_',collapse=' and ')
paste0('X',1:5)
- ‘X1’
- ‘X2’
- ‘X3’
- ‘X4’
- ‘X5’
paste0('X',1:5,collapse=",")