str_length Function in R (Example)
This article illustrates how to return the length of a string with the str_length function in the R programming language.
The article contains these contents:
Let’s get started.
Example Data
First, we need to create a character string for the following example:
x <- c("my example string") # Create character string
In preparation for the example, we also need to install and load the stringr package to R:
install.packages("stringr") # Install stringr package library("stringr") # Load stringr package
Example: Application of str_length Function in R
We can use the str_length function as shown below:
str_length(x) # Apply str_length function # 17
The output printed to the RStudio console is 17. In other words: Our string contains of 17 characters.
Note: The str_length function is very similar to the nchar function. You may also have a look at nchar in order to decide which function is better suited for you.
Video, Further Resources & Summary
In case you need more info on the R codes of this tutorial, you could watch the following video tutorial of my YouTube channel. In the video, I’m explaining the R programming code of this tutorial.
The YouTube video will be added soon.
In addition, you may want to read the related articles on my homepage. I have published numerous articles about the handling of character strings already.
This tutorial illustrated how to extract the number of characters in a string in the R programming language. In case you have additional questions or comments, please 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.






