str_flatten Function in R (Example)

 

In this article you’ll learn how to flatten a string with the str_flatten function in the R programming language.

The article looks as follows:

Let’s start right away.

 

Creation of Exemplifying Data

Before we can apply the str_flatten function, we need to create a vector of character strings:

x <- c("first string", "abc", "another string")    # Create vector of strings

Furthermore, we need to install and load the stringr package to R:

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

Now, we can move on to the example!

 

Example: Application of str_flatten Function in R

We can use the str_flatten function as shown below:

str_flatten(x, collapse = "---")                   # Apply str_flatten function
# "first string---abc---another string"

As you can see based on the output of the RStudio console, the str_flatten function concatenated our character strings by inserting “—” between each string.

You may change the collapse argument to your specific needs.

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which explains the R codes of this tutorial. Please find the video below.

 

The YouTube video will be added soon.

 

In addition to the video, you may want to read the related tutorials of this website:

 

In summary: On this page, I showed how to flatten a string in the R programming language. Tell me about it in the comments, in case you have further questions. Besides that, please subscribe to my email newsletter to receive regular updates on new tutorials.

 



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