. Advertisement .
..3..
. Advertisement .
..4..
Concatenating strings in R is one of the best ways to append at least two strings into a single one. These strings can be in the form of character by character or include special characters.
There are numerous methods to perform string concatenation. This tutorial will introduce you to the most simple and effective ones to accomplish the concatenating task.
How To Concatenate Strings In R
Use The Paste() Function
Using the paste()
function is one of the most widely used methods to concatenate different strings in R. This function’s syntax includes the following types of arguments:
Syntax:
paste(string1, string2, ….stringn, sep = “”, collapse=NULL)
Parameters:
- sep: this indicates the separator you are using.
- collapse: it shows how to get the element-wise result in the string’s vector.
- string1, string2, string3: these are characters for concatenation.
To concatenate or add various strings together, you need to use a specific separator within the paste()
function. It will serve as a collapse option.
In this example, we will make use of the default separator to do the task:
Code:
# Example R program to concatenate two strings
str1 = 'Hello'
str2 = 'World!'
# concatenate two strings using paste function
result = paste(str1,str2)
print (result)
Output:
$ Rscript r_strings_concatenate_two.R
[1] "Hello World!"
As can be seen from the code, this type of separator is the blank space or “ “. For this reason, the words “Hello” and “World!” will be combined with a space.
What if you cannot call for a separator? In this case, you can consider the empty string’s default value.
Code:
# Example R program to concatenate two strings
str1 = 'Hello'
str2 = 'World!'
# concatenate two strings using paste function
result = paste(str1,str2,sep="")
print (result)
Output:
$ Rscript r_strings_concatenate_two.R
[1] "HelloWorld!"
In this example, the default separator is overwritten with the blank space “”. That’s why there is nothing between the words “Hello” and “World!”.
The paste()
function is also useful to concatenate multiple strings together. The separator here is a hyphen or sep= “-”.
# Example R program to concatenate two strings
str1 = 'Hello'
str2 = 'World!'
str3 = 'Join'
str4 = 'Me!'
# concatenate two strings using paste function
result = paste(str1,str2,str3,str4,sep="-")
print (result)
Output:
$ Rscript r_strings_concatenate_multiple.R
[1] "Hello-World!-Join-Me!"
When using the paste()
function, you can also combine it with the collapse function to concatenate the strings. Yet, the method requires you to create a string vector and pass it as a parameter to the original method.
Input:
String1 <- 'Hello'
String2 <- 'World!'
String3 <- 'This'
String4 <- 'is'
String5 <- 'ITtutoria.net'
String_Vec <- cbind(String1, String2, String3, String4, String5) # combined vector.
Result1 <- paste(String1, String2, String3, String4, String5, sep ="")
print(Result1)
Result2 <- paste(String_Vec, collapse = ":")
print(Result2)
Output:
[1] "HelloWorld!ThisisITtutoria.net"
[1] "Hello:World!:This:is:ITtutoria.net"
Use The cat() Function
You can use the cat() function to perform string concatenation. This method allows you to perform concatenating with optimal flexibility to save a result.
Syntax: cat(string1, string2, …string, sep = ”, file, append)
Parameters:
- sep: it is the used separator.
- append: this is a logical argument so that your result can be appended in an existing file or you can create a new one.
- file: use this parameter to save the file’s result with a specified name.
- string1, string2, string3,…: like the previous method, these are strings for concatenation.
In the cat()
function, you will insert a string’s variable name and specify the separator. The method also allows you to insert more than two variables and different parameters at once.
Input:
# create 2 string variables
a <- 'Competitive'
b <- 'Coding'
c <- 'is difficult'
cat(a, b, c, sep = ' ')
Output:
Competitive Coding is difficult
After getting the concatenated result, you can easily save the file’s result with this method. The append here is often false by default. All you need is to specify append as TRUE to append the existing file’s result.
You will receive a specified file format:
# create a list of 10 numbers and
# save it into file named temp.csv
cat(11:20, sep = '\n', file = 'temp.csv')
readLines('temp.csv') # read the file temp.csv
Conclusion
Overall, there are various methods to concatenate strings in R. Depending on your data, you should choose a suitable method.
Leave a comment