Function nchar - Count the Number of Characters (or Bytes or Width)
nchar takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x.
Usage
nchar(x, type = “chars”, allowNA = FALSE, keepNA = NA)
nzchar(x, keepNA = FALSE)
Arguments
- x character vector, or a vector to be coerced to a character vector. Giving a factor is an error.
- type character string: partial matching to one of c(“bytes”, “chars”, “width”). See ‘Details’.
- allowNA logical: should NA be returned for invalid multibyte strings or “bytes”-encoded strings (rather than throwing an error)?
- keepNA logical: should NA be returned when x is NA? If false, nchar() returns 2, as that is the number of printing characters used when strings are written to output, and nzchar() is TRUE. The default for nchar(), NA, means to use keepNA = TRUE unless type is “width”.
Examples
x <- c("asfef", "qwerty", "yuiop[", "b", "stuff.blah.yech")
nchar(x)
# 5 6 6 1 15
nchar(deparse(mean))
# 18 17 <-- unless mean differs from base::mean
## NA behaviour as function of keepNA=* :
logi <- setNames(, c(FALSE, NA, TRUE))
sapply(logi, \(k) data.frame(nchar = nchar (NA, keepNA=k),
nzchar = nzchar(NA, keepNA=k)))
x[3] <- NA; x
nchar(x, keepNA= TRUE) # 5 6 NA 1 15
nchar(x, keepNA=FALSE) # 5 6 2 1 15
stopifnot(identical(nchar(x ), nchar(x, keepNA= TRUE)),
identical(nchar(x, "w"), nchar(x, keepNA=FALSE)),
identical(is.na(x), is.na(nchar(x))))
##' nchar() for all three types :
nchars <- function(x, ...)
vapply(c("chars", "bytes", "width"),
function(tp) nchar(x, tp, ...), integer(length(x)))
nchars("\u200b") # in R versions (>= 2015-09-xx):
## chars bytes width
## 1 3 0
data.frame(x, nchars(x)) ## all three types : same unless for NA
## force the same by forcing 'keepNA':
(ncT <- nchars(x, keepNA = TRUE)) ## .... NA NA NA ....
(ncF <- nchars(x, keepNA = FALSE))## .... 2 2 2 ....
stopifnot(apply(ncT, 1, function(.) length(unique(.))) == 1,
apply(ncF, 1, function(.) length(unique(.))) == 1)