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