String Concatenation in R Programming - GeeksforGeeks

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.
# create 2 different strings.string1 <- "Geeks" # first stringstring2 <- "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"
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 stringstring2 <- "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
With the help of paste() function we can concatenate more than two strings.
# create 3 different stringsstring1 <- "I" # first stringstring2 <- "Love" # second stringstring3 <- "Programming" # third string.  # perform concatenation on multiple strings.S <- paste(string1, string2, string3, sep =" ")print(S) # print the output string.
Output:
I Love Programming
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.
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 variablesa <- 'Competitive'b <- 'Coding'c <- 'is difficult'cat(a, b, c, sep = ' ')
Output:
Competitive Coding is difficult
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.csvcat(11:20, sep = '\n', file = 'temp.csv')readLines('temp.csv') # read the file temp.csv
Output:
Picked
R String-Functions
R-strings
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
Loops in R (for, while, repeat)
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
K-Means Clustering in R Programming
Remove rows with NA in one column of R DataFrame