Skip to content
Related Articles

Related Articles

String Concatenation in R Programming
  • Last Updated : 01 Jun, 2020

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




# 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

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.




# 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.

Output:

Hello World
Hello, World

Concatenating more than two strings

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




# 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.

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.




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

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.




# 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

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :