String Concatenation in R Programming

String concatenation is a way of appending two or more strings into a single string whether it is character by character or using some special character end to end. There are many ways to perform string concatenation.

Example:

Input: str1 = 'Geeks'
str2 = 'for'
str3 = 'Geeks' 
Output: 'GeeksforGeeks'

To perform concatenation in R we use the paste() function which can combine two or more strings together. It takes different types of arguments as follows:

Syntax:
paste(string1, string2, ….stringn, sep = “”, collapse=NULL)

Parameters:
string1, string2…string3: Strings provided for concatenation.
sep: the type of separator we use.
collapse: defines how to separate the result element-wise in the vector of string.



Concatenate two strings

filter_none

edit
close

play_arrow

link
brightness_4
code

# create 2 different strings.
string1 <- "Geeks" # first string
string2 <- "forGeeks" # second string.
  
# use cbind method to bind the strings into a vector.
vec <- cbind(string1, string2) # combined vector.
  
# use paste() function to perform string concatenation.
S <- paste(string1, string2, sep ="")
  
print(S)# print the output string
  
# collapse will determine how different
# elements are joined together.
M <- paste(vec, collapse = "#")
print(M) # print the output string using the collapse

chevron_right


Output:

"GeeksforGeeks"
"Geeks#forGeeks"

Concatenation using different types of separator

With the help of paste() function to we can perform concatenation by using different types of separators like whitespace or some special characters.

filter_none

edit
close

play_arrow

link
brightness_4
code

# create 2 strings.
string1 <- "Hello" # first string
string2 <- "World" # second string
  
# concatenate by using whitespace as a separator.
S <- paste(string1, string2, sep =" ")
print(S)
  
M <- paste(string1, string2, sep =", ")
print(M)# concatenate by using ', ' character.

chevron_right


Output:

Hello World
Hello, World

Concatenating more than two strings

With the help of paste() function we can concatenate more than two strings.

filter_none

edit
close

play_arrow

link
brightness_4
code

# create 3 different strings
string1 <- "I" # first string
string2 <- "Love" # second string
string3 <- "Programming" # third string.
  
# perform concatenation on multiple strings.
S <- paste(string1, string2, string3, sep =" ")
print(S) # print the output string.

chevron_right


Output:

I Love Programming

cat() Function

Similarly, we can perform concatenation using cat() function with the help of which we can perform character-wise concatenation and it also gives us the flexibility to save the result in a file.

Syntax:
cat(string1, string2, …string, sep = ”, file, append)

Parameters:

string1, string2…string3: Strings provided for concatenation.
sep: the type of separator we use.
file: used to save the result in a file with a specified name.
append: logical argument to specify if you want to append the result in an existing file or create a new file.

Concatenating two or more strings using cat() function.

We can perform concatenation by inserting the variable name of the string and specifying the separator.we can insert more than two variables also and specify different types of parameter.

filter_none

edit
close

play_arrow

link
brightness_4
code

# create 2 string variables
a <- 'Competitive'
b <- 'Coding'
c <- 'is difficult'
cat(a, b, c, sep = ' ')

chevron_right


Output:

Competitive Coding is difficult

Saving the concatenated result in a file

To save the result of the concatenated output we can specify the name of the file in the argument. By default the append is false, but if you want to append the result into an existing file then specify append as TRUE.
The result will be saved into the specified file format.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:




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.