# 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" |

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" |

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" |

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" |

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 |

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" |

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!

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R