String Concatenation in R

String Concatenation is the process of merging two or more strings. For example, the concatenation of “poke” and “mon” is “pokemon”. This guide will show the process of string concatenation in R.

String Concatenation using paste() method

paste() method allows you to merge two or more strings by separating them with delimiters.

Syntax:

paste(string1, string2, sep=)

Here, the “sep” argument is optional. It denotes a character string to separate string variables. It can be a dash, space, hyphen, or any other character that wants to interject between the string variables. By default, string variables are separated by whitespace as a separator.

Example:

#creating strings
string1 <- "I" #first string
string2 <- "Love" #second string
string3 <- "R" #third string
string4 <- "Programming" #fourth string
#concatenating using paste() function
paste(string1,string2,string3,string4, sep=" ")
#concatenating with a different separator
paste(string1,string2,string3,string4, sep="-")

Output:

Output
String Concatenation using paste() function.

Was this article helpful?

Leave a Comment