How to Concatenate Strings in R

[This article was first published on R – ItsMyCode, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

How to Concatenate Strings 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 Strings using the paste() method in R

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

The paste() method can take multiple strings as an 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 in R using the paste() method

The paste() method accepts two or more string 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 with a separator in R using the paste() method

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 in R

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 in R 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 in R with a separator using the cat() method

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 in R and output 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 in R 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.

To leave a comment for the author, please follow the link and comment on their blog: R – ItsMyCode.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)