The R programming language comes with a number of built-in functions to work with character strings. In this tutorial, we will look at one such function that helps us get the length of a string in R with the help of some examples.

## How to find the length of a string in R?

You can use the built-in `nchar()`

function to get the length of a string in R. Pass the string as an argument to the function.

The following is the syntax –

# length of string s nchar(s)

The `nchar()`

function returns the number of characters in the string (which is the same as the length of the string).

## Examples

Let’s now look at some examples of using the above syntax.

### Length of a string using `nchar()`

Let’s apply the `nchar()`

function on a string, for example, “Avengers Endgame”.

# create a string s <- "Avengers Endgame" # get length of s print(nchar(s))

Output:

[1] 16

We get 16 as the length of the string “Avengers Endgame”. This is the correct length since there is a total of 16 characters in the above string.

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

### Length of each string in a vector

If you apply the `nchar()`

function to a string vector, it will return a vector with the length of each corresponding string in the vector.

Let’s look at an example.

# create a vector of strings vec <- c("you", "are", "my", "fire") # get length of each string in vec print(nchar(vec))

Output:

[1] 3 3 2 4

You can see that we get the length of each string the vector `vec`

with the `nchar()`

function.

What if the string vector has `NA`

values?

Let’s find out.

This time, we’ll add some `NA`

values to the above vector and then apply the `nchar()`

function.

# create a vector of strings with some NA values vec <- c("you", "are", NA, "my", "fire", NA) # get length of each string in vec print(nchar(vec))

Output:

[1] 3 3 NA 2 4 NA

We get the length for each valid string and `NA`

for the `NA`

s in the above vector.

You might also be interested in –

- Compare Two Strings in R (With Examples)
- R – Remove Whitespaces From String
- Convert Numeric Type to Character Type in R

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**