5 Ways to Combine Strings in R

There are several ways to combine strings, also known as concatenation strings in R, and in today’s article, we will see all the different ways.

Combine Strings in R

To combine strings in R, you can use the five different ways.

  1. Using the paste() function: It combines or concatenates two or more strings.
  2. Using the cat() function: It concatenates and prints the combined strings in the console.
  3. Using the paste0() function: It concatenates the strings without any separator.
  4. Using str_c() function: It combines multiple strings into a single string in R.
  5. Using the sprintf() function: It combines strings by defining placeholders in a format string and providing the values as separate arguments.

Method 1: Using the paste() function

You can use the paste() function to combine two or more strings in R. The paste() method takes three parameters and returns concatenated string.

string_first <- "Ellie"
string_second <- "Joel"
string_third <- "Tommy"

string_combine <- paste(string_first, string_second, string_third)
print(string_combine)

Output

[1] "Ellie Joel Tommy"

You can see we combined three strings using the paste() function.

Passing a separator to the paste() function

With the help of the paste() function, we can combine strings using different types of separators like whitespace or some special characters.

Let’s pass the “,” as a separator to the paste() function.

string_first <- "Ellie"
string_second <- "Joel"
string_third <- "Tommy"

string_combine <- paste(string_first, string_second, string_third, sep = ",")
print(string_combine)

Output

[1] "Ellie,Joel,Tommy"

Method 2: Using the cat() function

Use the built-in cat() function that concatenates, combines, and prints strings in the R console.

With the help of the cat() function, we can perform character-wise concatenation, giving us flexibility.

string_first <- "Ellie"
string_second <- "Joel"
string_third <- "Tommy"

cat(string_first, string_second, string_third, "\n")

Output

Ellie Joel Tommy

The cat() function combines the values of the variables string_first, string_second, and string_third and prints the result to the console.

The \n character at the end of the command is a unique character that adds a new line to the end of the output.

Method 3: Using the paste0() function

The paste0() in R is a built-in function used to concatenate the strings without adding any separator.

string_first <- "Ellie"
string_second <- "Joel"
string_third <- "Tommy"

paste0(string_first, string_second, string_third)

Output

[1] "EllieJoelTommy"

Method 4: Using the str_c() function

The str_c() function from the stringr library combines multiple strings into a single string in R.

library(stringr)

string_first <- "Ellie"
string_second <- "Joel"
string_third <- "Tommy"

str_c(string_first, string_second, string_third)

Output

[1] "EllieJoelTommy"

The str_c() function can be used to concatenate strings by specifying the strings to be combined as separate arguments.

Method 5: Using the sprintf() function

The sprintf() function combines strings by defining placeholders in a format string and providing the values as separate arguments.

sprintf("Ellie %s %s", "Joel", "Tommy")

Output

[1] "Ellie Joel Tommy"

Conclusion

Use the paste() or cat() function to combine two or more strings in R. Alternate ways are using the paste0() and str_c() functions to combine strings.

Leave a Comment