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:
- Creation of Exemplifying Data
- Example: Application of str_flatten Function in R
- Video, Further Resources & Summary
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:
- Concatenate Vector of Character Strings in R
- R Graphics Gallery
- R Functions List (+ Examples)
- The R Programming Language
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.
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.






