VOOZH about

URL: https://www.geeksforgeeks.org/r-language/finding-the-length-of-string-in-r-programming-nchar-method/

⇱ Finding the length of string in R programming - nchar() method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Finding the length of string in R programming - nchar() method

Last Updated : 28 Apr, 2025

nchar() method in R Programming Language is used to get the length of a character in a string object.

Syntax: nchar(string)

Where: String is object.

Return: Returns the length of a string. 

R - length of string using nchar() Example

Example 1: Find the length of the string in R

In this example, we are going to see how to get the length of a string object using nchar() method.

Output:

[1] 15

Example 2: Use nchar for R Vector

In this example, we will get the length of the vector using nchar() method.

Output:

'character'
5 1 5 2

Example 3: Passing NA values to the nchar() function

The nchar() function provides an optional argument called keepNA, it can help when dealing with NA values.

Output:

1 5 2

In the above example, the first element is NULL then it returns nothing and the last element NA returns 2 because we keep keepNA = FALSE. If we pass keepNA = TRUE, then see the following output:

Output:

0 5 <NA>
Comment
Article Tags:
Article Tags:

Explore