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:

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:

 

Paste R Function R Documentation Help

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]

An alternative to the base R solution of paste() is the str_c function of the stringr Package. Before we can use the str_c command in R, we need to install and load the 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

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!

 



 

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top