Concatenate Vector of Character Strings in R (2 Examples) | How to Combine Text Cases
In this article, I’ll explain how to concatenate a vector of character strings in the R programming language. The table of contents is as follows:
- Creating Example Character String
- Concatenate Vector of Character Strings with paste Function (Example 1)
- Concatenate Strings with str_c Function of stringr Package (Example 2)
- Further Resources for the Handling of Character Strings in R
Let’s dive in…
Create Example Data
In the examples of this tutorial, I will use the following vector of character strings:
x <- c("hello", "this", "should", "be", "concatenated") # Create example vector x # Print example vector to console # "hello" "this" "should" "be" "concatenated"
Our example vector is called x and contains five character strings.
In the following, I’ll show you in several examples how to combine these character strings into one single character string.
So let’s move on to the examples…
Example 1: Concatenate Vector of Character Strings with paste Function
Typically, character strings are concatenated with the paste function. In order to add several strings together, we need to specify a certain separator for the collapse option within the paste function.
Let’s first have a look at the help documentation of the paste function in R:
Figure 1: R Help Documentation of paste Function.
As you can see in Figure 1, the collapse option of the paste function is what we are looking for.
So let’s apply this in practice: With this R code, we are separating our example vector with blanks…
paste(x, collapse = " ") # Collapse example character string # "hello this should be concatenated"
…with this code we are separating with an underline…
paste(x, collapse = "_") # Collapse with different separator # "hello_this_should_be_concatenated"
…and with this code we are just pasting everything together without a separator:
paste(x, collapse = "") # Collapse without separator # "hellothisshouldbeconcatenated"
With the collapse option, you can specify every character value you want as separator.
In my opinion, the paste function provides the best solution for the concatenation of character strings. However, for completion I want to show you another alternative in the next example…
Example 2: Concatenate Vector of Character Strings with str_c Function [stringr Package]
install.packages("stringr") # Install stringr package library("stringr") # Load stringr package
Now we can apply str_c in a similar manner as paste:
str_c(x, collapse = " ") # Collapse string with str_c # "hello this should be concatenated"
Further Resources for Dealing with Character Strings
Do you want to see another example for the concatenation of character strings in R? Then you could have a look at the following video of the Mr. Math Expert YouTube channel.
In the video, the speaker explains how you could use the paste function without specifying the collapse option in order to combine several strings. He simply inserts each character string as separate value into the paste function.
Besides the video, you may also have a look at the following tutorials of this website. I have already published several tutorials on the handling of character strings in R
- str_c R Function
- Extract First or Last Characters from String
- Convert Character to Numeric in R
- How to Replace the Last Comma of a Character String
- Count Number of Characters in String
- List of R Commands (+ Examples)
- The R Programming Language
After reading this tutorial, you should know how to concatenate two or more character strings in R. However, in case you are still struggling with this topic, don’t hesitate to let me know in the comments!