How to count the number of characters (or bytes or width)

Published

May 10, 2022

From the documentation, nzchar() is a fast way to find out if elements of a character vector are non-empty strings. It returns TRUE for non-empty strings and FALSE for empty strings.

# This is not empty
Sys.getenv("R_LIBS_USER")
[1] "/Users/ivelasq/Library/R/x86_64/4.3/library"
# This returns TRUE
nzchar(Sys.getenv("R_LIBS_USER"))
[1] TRUE
# This is empty
Sys.getenv("test")
[1] ""
# This returns FALSE
nzchar(Sys.getenv("test"))
[1] FALSE