str_trunc Function in R (Example)
In this tutorial you’ll learn how to truncate character strings with the str_trunc function in the R programming language.
Table of contents:
Here’s the step-by-step process:
Example Data
Consider the following example character string in R:
x <- "heyho I am a string" # Create a character string
Then, we also need to install and load the stringr package of the tidyverse environment to R:
install.packages("stringr") # Install stringr R package library("stringr") # Load stringr R package
Example: str_trunc Function in R
In order to truncate our example string x, we can apply the str_trunc command as follows:
str_trunc(x, 10) # Apply str_trunc function # "heyho I..."
As you can see based on the output of the RStudio console, our character string was cut off after the 7th character and the last three characters where replaced by dots.
Video, Further Resources & Summary
If you need further info on the R programming syntax of this tutorial, you could watch the following video that I have published on my YouTube channel. In the video, I illustrate the content of this post:
The YouTube video will be added soon.
Furthermore, you could read the other tutorials of my homepage:
To summarize: At this point you should have learned how to reduce the length of character strings in the R programming language. In case you have any further questions and/or comments, let me know in the comments section.
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.






