# str_c R Function of stringr Package (3 Example Codes)

This R tutorial shows **how to use the str_c function** of the stringr package. Let’s first have a look at the basic R syntax and the definition of the str_c function:

**Basic R Syntax of str_c:**

str_c("char_a", "char_b") |

str_c("char_a", "char_b")

**Definition of str_c:**

The str_c function combines several inputs into a single character vector.

In the following article, I’ll show **three examples** for the usage of str_c in the R programming language. The examples will explain different options of str_c such as *sep* or *collapse*:

**Figure 1: Excerpt of the R Help Documentation of the str_c Function.**

So without further ado, let’s get started!

## Example 1: Basic application of str_c

The first example shows how str_c is used with default specifications. Before we can start with the example, we need to install and load the stringr R package:

install.packages("stringr") # Install stringr R package library("stringr") # Load stringr R package |

install.packages("stringr") # Install stringr R package library("stringr") # Load stringr R package

Now, we can use the str_c function as follows:

str_c("Hi", ",", "these", "are", "some", "strings") # Basic application # "Hi,thesearesomestrings" |

str_c("Hi", ",", "these", "are", "some", "strings") # Basic application # "Hi,thesearesomestrings"

As you can see, we gave several character strings as input to the str_c command. These character strings where joined without a separator to a single word.

By the way: Base R provides the paste & paste0 functions, which are very similar to str_c. In fact, paste0 would result in exactly the same output as the previous R code. If you want to learn more about the difference between str_c, paste & paste0, you should have a look at this R tutorial.

However, the next example will show you how to add a separator to the str_c R function. So keep on reading!

## Example 2: Specify Separator for str_c

With *sep = “”* we can specify how the input characters should be separated. In the following example, we are separating our input strings with a blank in between:

str_c("Hi", ",", "these", "are", "some", "strings", sep = " ") # str_c with sep # "Hi , these are some strings" |

str_c("Hi", ",", "these", "are", "some", "strings", sep = " ") # str_c with sep # "Hi , these are some strings"

## Example 3: Combine vector of strings with str_c & collapse Option

Assume that our character strings that we want to join together are stored as vector in some data object:

x <- c("Hi", ",", "these", "are", "some", "strings") # Create vector of strings |

x <- c("Hi", ",", "these", "are", "some", "strings") # Create vector of strings

If we apply str_c with default specifications, the function will return the same vector to the RStudio console:

str_c(x) # Apply str_c to vector # "Hi" "," "these" "are" "some" "strings" |

str_c(x) # Apply str_c to vector # "Hi" "," "these" "are" "some" "strings"

However, we can concatenate this character vector into a single character string with the collapse option:

str_c(x, collapse = " ") # collapse option of str_c # "Hi , these are some strings" |

str_c(x, collapse = " ") # collapse option of str_c # "Hi , these are some strings"

As you can see based on the quotes, the str_c printed only a single character string.

## Further Resources for the Handling of Character Strings

The character class is sometimes difficult to understand. For that reason, you might want to learn more about the handling of characters in general. In this case, I can definitely recommend the following video of the expresstut01 YouTube channel, which is explaining the stringr package in more detail:

In addition, I recommend having a look at the other R tutorials on this site. I have already published a decent amount of articles on the handling of strings in R:

- The paste & paste0 Functions in R
- str_sub R Function (stringr Package)
- How to Concatenate a Vector of Strings
- The R Programming Language

In summary: This tutorial explained how to handle the str_c function in R. However, if you have any comments or questions, don’t hesitate to let me know in the comment section below. I’ll respond to all comments as quick as I can!

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