The nchar Function: Everything You Need to Know


The nchar function is a built-in function in R that is used to determine the number of characters in a string. It is an essential function in data cleaning and manipulation, especially when dealing with text data. In this article, we’ll explain everything you need to know about the nchar function, including its syntax, usage, and examples.


The syntax for the nchar function is as follows:

nchar(x, type = "char", allowNA = NA)

  • x: A character vector, a list or an object coercible to a character vector.
  • type: The type of character being counted. The options are “char” (default) or “bytes”.
  • allowNA: Logical value, indicating whether or not to return NA when the input contains missing values.


The nchar function is primarily used to determine the number of characters in a string. It’s usually used in combination with other functions in the data cleaning and manipulation pipeline. For instance, it can be used to remove trailing or leading spaces in a string using the trimws function.


Let’s look at a few examples of how the nchar function can be used:

> example_str <- "Hello World!"

> nchar(example_str)

[1] 12

> example_vector <- c("Hadoop", "Spark", "Python")

> nchar(example_vector)

[1] 6 5 6

In the first example, the nchar function returns the number of characters in the example_str variable, which is 12. In the second example, the function is applied to a vector, which returns the number of characters in each element of the vector.


What is the difference between “char” and “bytes” in the type parameter of the nchar function?

The “char” option counts the number of characters in a string, while “bytes” counts the number of bytes used to store the string.

Can the nchar function be used to count the number of words in a string?

No, the nchar function counts the number of characters in a string, not the number of words.

What happens if I apply the nchar function to a missing value?

The default behavior is to return NA. You can change this behavior by setting the allowNA parameter to FALSE.

Similar Posts