The nchar R Function | 3 Examples (String, Vector & Error: nchar Requires a Character)


Basic R Syntax:



The nchar R function counts the number of elements (e.g. letters) of a character object. The basic syntax for nchar in R is illustrated above.

In the following tutorial, I will show you 3 examples how to apply the nchar command in R. So if you want to know more about nchar, keep reading…


Example 1: Count nchar of a String in R

Typically, nchar is used for a single string (in R usually called character object). As example, let’s create such a character object in R:

x <- "Hello R User"                        # Create example character object

To this character object, we can now apply the nchar R function:

nchar(x)                                   # Apply nchar in R
# 12

The nchar command returns the value 12 – Our character object has 12 elements.

Note: The nchar function also counts spaces, special characters etc.


Example 2: Use nchar for R Vector

The nchar function can also be applied to a character vector. Let’s create such a vector:

x_vec <- c("Lalala Lololo", "12345",       # Create example character vector
           "1 2 3 4 5", "?=%$§-+????")

Now, let’s apply nchar to this vector:

nchar(x_vec)                               #  Apply nchar to character vector
# 13  5  9 11

As you can see, nchar returns 4 values – One value for each entry of our input vector. The first entry of the input vector consists of 13 elements; the second entry consists of 5 elements; and so on…


Example 3: Error in nchar(x) : ‘nchar()’ Requires a Character Vector

There is one error message that R users report regularly:


R Error Message: nchar Requires a Character Vector

Graphic 1: Error – nchar Requires a Character Vector? Don’t Panic!


But when does this happen? And how to solve this problem? That’s exactly what I’m going to show you now…

First, let’s create another data object – but this time a factor:

x_fac <- as.factor("Fac1")                  # Create example factor

Now, let’s see what happens when we use the nchar function:

nchar(x_fac)                                # Apply nchar to factor
# Error in nchar(x) : 'nchar()' requires a character vector

You guessed it… The RStudio console returns exactly the previously described error message. So what to do now?

Simply convert your factor to character or numeric, before you apply the nchar function:

nchar(as.character(x_fac))                  # Solution
# 4

That’s it!

Note: Characters in R are often automatically converted to a factor, e.g. when you add them as column to a data frame. If R returns the previous error message, check carefully if all your data has the format that it should have.


Video Explanation: The R nchar Function

Do you need more explanations and examples? Have a look at the following video of the YouTube channel Dragonfly Statistics.



Further Reading



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.