Finding the length of string in R programming - nchar() method Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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() ExampleExample 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. R # R program to calculate length of string # Given String gfg < - "Geeks For Geeks" # Using nchar() method answer < - nchar(gfg) print(answer) Output: [1] 15Example 2: Use nchar for R Vector In this example, we will get the length of the vector using nchar() method. R # R program to get length of Character Vectors # by default numeric values # are converted into characters v1 <- c('geeks', '2', 'hello', 57) # Displaying type of vector typeof(v1) nchar(v1) Output: 'character' 5 1 5 2Example 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. R # R program to create Character Vectors # by default numeric values # are converted into characters v1 <- c(NULL, '2', 'hello', NA) nchar(v1, keepNA = FALSE) 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: R # R program to create Character Vectors # by default numeric values # are converted into characters v1 <- c('', NULL, 'hello', NA) nchar(v1, keepNA = TRUE) Output: 0 5 <NA> Comment More infoAdvertise with us Next Article Finding the length of string in R programming - nchar() method J Jitender_1998 Follow Improve Article Tags : R Language Similar Reads Getting and Setting Length of the Vectors in R Programming - length() Function In R, the length() function is used to determine the number of elements in a vector. Vectors can be of various types, such as numeric, character, or logical, and the length() function provides a simple way to find out how many elements are contained in a vector. This function is versatile and can al 5 min read How to find SubString in R programming? In this article, we are going to see how to find substring in R programming language. R provides different ways to find substrings. These are: Using substr() methodUsing str_detect() methodUsing grep() methodMethod 1: Using substr() method Find substring in R using substr() method in R Programming 2 min read String Matching in R Programming String matching is an important aspect of any language. It is useful in finding, replacing as well as removing string(s). In order to understand string matching in R Language, we first have to understand what related functions are available in R. In order to do so, we can either use the matching str 5 min read Get the number of rows of an Object in R Programming - nrow() Function nrow() function in R Language is used to return the number of rows of the specified matrix. Syntax: nrow(x)Parameters: x: matrix, vector, array or data frame  Example 1:  Python3 # R program to illustrate # nrow function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling nrow() function 1 min read R Program to Count the Number of Vowels in a String In this article, we will discuss how to Count the Number of Vowels in a String with its working example in the R Programming Language. It is a fundamental in programming that allows us to repeatedly execute a block of code as long as a specified condition remains true. It's often used for tasks like 5 min read Like