• Post author:
  • Post category:R Programming
  • Post last modified:March 27, 2024
  • Reading time:12 mins read
You are currently viewing How to Concatenate the String in R?

How to concatenate the strings in R? You can use the paste() function or the paste0() function to concatenate the strings. paste() is used to concatenate strings with both a space separator(default) and a custom separator. Whereas paste0() function to concatenate the strings without any separator.

Advertisements

In this article, I will explain the various base functions of R such as <a href="http://paste()">paste()</a>, paste0(), cat(), and sprintf() functions, and using these functions how we can perform the concatenation operations on two or more strings into a single character vector.

Related: You can remove all white space from the string in R.

Key points-

  • The paste() function is used for concatenating strings in R.
  • It allows the concatenation of two or more strings or expressions into a single character vector.
  • The sep parameter specifies the character string that separates consecutive elements during concatenation.
  • If sep is not specified, the default separator is a white space.
  • The collapse parameter is used to collapse the result into a single string, inserting the specified string between each element.
  • The paste() function is flexible and can be used in various scenarios, including specifying custom separators and collapsing results.
  • The paste0() function is a shorthand for paste(..., sep = ""), concatenating strings without any separator.

Concatenate R Stirngs using paste() function

In R, paste() is a versatile function to concatenate two or more strings. By default, it concatenates the strings by white space(" ") and returns a character vector. If you want to customize the separator you can use the sep parameter of the paste() function

Syntax of the paste() function

Following is the syntax of the paste() function.


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

Parameters

  • string1, string2…stringn: (Required)Strings are used to perform concatenation. 
  • sep:(Optional) A character string that separates the strings (default is a space)
  • collapse:(Optional) An optional character string to collapse the result into a single string.

Return Value

It returns a character vector which contains concatenated values.

Let’s create two strings'SparkBy' and 'Examples', and demonstrate concatenation using paste() function. First, pass the given strings into the paste() function by default, it will merge the given strings with a white space(” “) separator.


# Concatenate the strings using paste()
# Create two strings
str1 <- "SparkBy"
str2 <- "Examples"
con_str <- paste(str1, str2)
print("After concatenating the strings:")
print(con_str)

Yields below output.

 string concatenate in r

Alternatively, in R, you can concatenate the strings into a single string with a specified separator. To do this you can simply set and pass the sep parameter into the paste() function along with the strings you want to merge. It will merge the strings into a single string with a specified separator.


# Concatenate the strings with custome separator
# Create two strings
str1 <- "SparkBy"
str2 <- "Examples"
con_str <- paste(str1, str2, sep = ", ")
print(con_str)

Yields below output.

 string concatenate in r

Concatenate Strings in R without Space

paste0() function of R used to concatenate two or more strings into a single string without any separator. It takes the input strings and concatenates them into a single string without using any separator. Using the optional parameter collapse you can insert the specified string between each element after the result.

Syntax of paste0() Function

Following is the syntax of the paste0() function.


# Sytax of the paste0() function
paste0(string1, string2, ….stringn, collapse = NULL)

Parameters

  • string1, string2…stringn: The input vectors or expressions for concatenation.
  • collapse:(optional) An optional character string to collapse the result into a single string.

Return Value

It returns the character vector of the concatenated values without any separator.

Let’s use the paste0() function to combine the given strings and demonstrate concatenation.


# Concatenate the strings using paste0()
# Create the strings
str1 <- "SparkBy"
str2 <- "Examples"
con_str <- paste0(str1, str2)
print("After concatenating the strings:")
print(con_str)

# Output:
# [1] "SparkByExamples"

The above code has returned the character vector named con_str which has no space in between after completing the concatenation of two strings named str1, str2.

Concatenate R Strings Using the cat()

Similarly, you can use the cat() function to concatenate two or more strings into a single string in R. If we pass the input strings with no separator by default, this function will return the character vector with a white space separator. This behavior is the same as the paste() function. Using this function you can can also save the concatenation result in a file.


# Concatenate the strings using cat()
# Create the strings
str1 <- "Welcome"
str2 <- "To"  
str3 <- "SparkBy"
str4 <- "Examples"
str = c(str1, str2, str3, str4)
con_str <- cat(str,  sep = ",")

# Output:
# Welcome,To,SparkBy,Examples

You can save the concatenation result of a cat()function in a specified file. Here, I will store this result in a CSV file.


# Save the result of a cat() function into a csv file.
con_str <- cat(str,  sep = "\n ", file = "C:\\Users\\B.VIJETHA\\OneDrive\\Desktop\\file.csv")

Yields below output.

 string concatenate in r

Use str_c() to Concatenate Strings in R

You can also use the str_c() function from the stringr package of R to concatenate two or more strings. Before going to use this function we need to install this package into our R environment like install. packages("stringer"). Once the installation is complete, you can load the package using the library(stringer).


# Concatenate the strings using str_c()
# from stringer package
library(stringr)
# Create the strings
str1 <- "Welcome"
str2 <- "To"  
str3 <- "SparkBy"
str4 <- "Examples"
str = c(str1, str2, str3, str4)
con_str <- str_c(str)
con_str

# Output:
# [1] "Welcome"  "To"       "SparkBy"  "Examples"

Using the sprintf() function

Finally, you can use the sprintf() function to format a string by performing a concatenation operation. The format string "%s %s %s %s" specifies a format with four placeholders, each represented by white space.


# Concatenate the strings using sprintf()
# Create the strings
str1 <- "Welcome"
str2 <- "To"  
str3 <- "SparkBy"
str4 <- "Examples"
con_str <- sprintf("%s %s %s %s", str1, str2, str3, str4)
con_str

# Output:
# "Welcome To SparkBy Examples"

Conclusion

In this article, I have explained the paste() function and paste0() functions of R and using these how we can concatenate the two more strings into a character vector with and without separators. Also implemented the concatenation of the strings using various functions and packages of R such as cat(), str_c(), and sprintf() functions.

Happy learning!!

Related Articles