What is nzchar() Function in R

The nzchar() is a built-in R function that tests whether elements of a character vector are non-empty strings.

Syntax

nzchar(x)

Parameters

The nzchar() function takes the character vector x as a parameter.

Return Value

It returns the boolean value either TRUE or FALSE.

Example

Let’s define a character vector and test whether the vectors’ elements are non-strings.

data <- c("Avada Kedavara", "Crucio", "Expecto Patronum")
nzchar(data)

Output

[1] TRUE TRUE TRUE

In this example, all the vector elements are non-empty strings. So it returns TRUE to all the elements.

Test the following.

data <- c("", "Aloh Mora", "")
nzchar(data)

Output

[1] FALSE TRUE FALSE

As you can see from the output, if the character element is empty, it returns FALSE otherwise TRUE. The nzchar() function is the fastest way to determine if components of a character vector are non-empty strings.

Leave a Comment