The nzchar() is a built-in R function that tests whether elements of a character vector are non-empty strings.
The nzchar() function takes the character vector x as a parameter.
It returns the boolean value either TRUE or FALSE.
Let’s define a character vector and test whether the vectors’ elements are non-strings.
data <- c("Avada Kedavara", "Crucio", "Expecto Patronum") nzchar(data)
 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)
 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.
Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.