The cat() function to concatenate or merge objects in R.
The following method shows how you can do it with syntax.
Method: Use cat() Function
cat(value1,value2,..., file = "file_name", sep = " ", append = FALSE)
Here we pass values,file name in which store values,seperated by symbol and append is used to whether to append output to existing file or create new file.
The following examples show how to use cat() function in R.
Use cat() Function to Concate Object
Let’s see example how we can concat strings using cat() function.
# Concat objects
cat("This","is","R","language")
Output:
This is R language
The output shows strings are concated using cat() function.
Use cat() Function to Concate Object with Separator
You can concate objects using cat() function with separator:
# Concat strings and seperated by ,
cat("This","is","R","language",sep=",")
Output:
This,is,R,language
Here the output shows objects are concated using cat() function and separated by ,.
Let’s see another example how we can concate object with \n separator.
# Concat strings and seperated by \n
cat("This","is","R","language",sep="\n")
Output:
This
is
R
language
The output shows strings are concated and each string printed on next line.
Use cat() Function to Concate Object with Separator and Store in File
Let’s see how we can concate object and store in file:
# Concat strings which seperated by \n and store output in file
cat("This","is","R","language",sep="\n", file = "data.txt")
Here in the above code we pass strings which are seperated by “\n” to cat() function also we pass file name to it to store strings in it.While storing strings in file we store each string in new line.