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


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:


str_c R Help Documentation stringr Package

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

Now, we can use the str_c function as follows:

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"


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

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"

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"

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


Tutorial Video & Further Resources for the Handling of Character Strings

Below, you can find a video on the Statistics Globe YouTube channel where I describe the steps of this tutorial in expanded detail:



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:

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 the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

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.