Monday, 27 July 2015

R: nchar : Get the length of string

“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)

x : Character vector

type : It is one of “chars”, “bytes”, “width”. If type is set to chars, it returns number of characters in string. If type is set to bytes, it returns the number of bytes needed to store the string. If type is set to width, it returns the number of columns cat will use to print the string in a monospaced font.

allowNA:  should NA be returned for invalid multibyte strings or "bytes"-encoded strings.

> x <- c("Hello", "Hari", "How", "Are", "you", "He", "Hat")
> nchar(x)
[1] 5 4 3 3 3 2 3
> 
> nchar(x, type="bytes")
[1] 5 4 3 3 3 2 3
> 
> nchar(x, type="width")
[1] 5 4 3 3 3 2 3



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment