How to Concatenate Strings in R

We can perform string concatenation in R language using the paste() and cat() methods.

In this tutorial, let us explore the various methods available to concatenate strings in r and how to use these methods with examples.

Different ways of String Concatenation in R

String concatenation is a process of joining two or more strings into a single string. R language provides two different methods for string concatenation.

Concatenate using the paste() method

paste() method is the most common and widely used method for string concatenation in R. 

The paste() method can take multiple strings as input, combine them, and return a concatenated string as an output.

Syntax

paste(string1, string2, …stringn, sep = “”, collapse=NULL)

Parameters

  • string1, string2, …stringn: Input strings that need to be concatenated.
  • sep: The separator that needs to be appended during the concatenation. If you don’t pass this argument, it will take space as the default separator.
  • collapse: an optional character string to separate the results. 

Example 1 – Concatenate two or more strings using the paste() method

The paste() method accepts two or more strings as input and returns a concatenated string as output.

 output1 <- paste("R","Programming")
 output2 <- paste("R","Programming","is","fun")
 output1
 output2

Output

"R Programming"
"R Programming is fun"

Example 2 – Concatenate strings using the paste() method with separator argument

In the first example, we did not pass a value to sep the argument and it used a default separator as whitespace while concatenating the string.

Let us extend our example to see how to pass a separator argument and concatenate the string in R.

 output1 <- paste("R","Programming",sep = '-')
 output2 <- paste("R","Python", "NumPy","Pandas",sep = ',')
 output1
 output2

Output

"R-Programming"
"R,Python,NumPy,Pandas"

Concatenate Strings using the cat() method

The cat() method works similarly to the paste() method. It can perform a character-wise concatenation and print the concatenated string as output, and also we can save the concatenated string into a file.

Syntax

cat(… , file = "", sep = " ", fill = FALSE, labels = NULL,
    append = FALSE)

Parameters

  • : R objects to concatenate
  • file (optional): The name of the file where the output is printed
  • sep (optional): The separator that needs to be appended during the concatenation. If you don’t pass this argument, it will take space as the default separator.
  • fill (optional): A logical or positive numeric which represents how the output will be broken into successive lines. The default value is false.
  • labels (optional): A character vector of labels for the lines printed. Ignored if fill is FALSE.
  • append (optional): Required when you print the output to the file. If we pass ‘TRUE‘ the output will be appended to the file otherwise it will overwrite the contents of the file.

Example 1 – Concatenate two or more strings using the cat() method

The cat() method accepts two or more objects as input and returns a concatenated string as output.

firstName <- "Chandler"
middleName <- "R"
lastName <- "Bing"
cat(firstName, middleName, lastName)

Output

Chandler R Bing

Example 2 – Concatenate Strings using the cat() method with separator argument

In the below example, we use the cat() method to concatenate two or more string objects and use a custom separator.

firstName <- "Chandler"
middleName <- "R"
lastName <- "Bing"
cat(firstName, middleName, lastName, sep = "-")
cat(firstName, middleName, lastName, sep = ",")

Output

Chandler-R-Bing
Chandler,R,Bing

Example 3 – Concatenate Strings and write the results to file using the cat() method

In the below example, we use the cat() method to concatenate two or more string objects and output the results to the CSV files, Text files, or any other file format.

The name.txt file gets created in the working directory.

firstName <- "Chandler"
middleName <- "R"
lastName <- "Bing"
cat(firstName, middleName, lastName, sep ="\n", file="name.txt")

Output

Image
String Concatenation in R

Example 4 – Concatenate Strings and append results to the file using the cat() method

In the below example, we use the cat() method to concatenate two or more string objects and append the results to the file.

The name.txt file gets created in the working directory.

firstName <- "Ross"
middleName <- "P"
lastName <- "Taylor"
cat(firstName, middleName, lastName, sep ="\n", file="name.txt" , append=TRUE)

Output

Image 1
Append concatenated results to file in R

Conclusion

String concatenation is a process of joining two or more strings into a single string. We can perform string concatenation in R using the paste() and cat() methods.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Calculate Standard Error In R

Calculate Standard Error in R

Table of Contents Hide How to Calculate Standard Error in R?Using sd() methodUsing the standard error formulaUsing std.error() method from plotrix The standard error (SE) of a statistic is the standard deviation of its sampling…
View Post