How to find the length of a string in R

寵の児 提交于 2019-12-17 15:03:22

问题


How to find the length of a string (number of characters in a string) without splitting it in R? I know how to find the length of a list but not of a string.

And what about Unicode strings? How do I find the length (in bytes) and the number of characters (runes, symbols) in a Unicode string?

Related Question:

  • How to find the "real" number of characters in a Unicode string in R

回答1:


See ?nchar. For example:

> nchar("foo")
[1] 3
> set.seed(10)
> strn <- paste(sample(LETTERS, 10), collapse = "")
> strn
[1] "NHKPBEFTLY"
> nchar(strn)
[1] 10



回答2:


Use stringi package and stri_length function

> stri_length(c("ala ma kota","ABC",NA))
[1] 11  3 NA

Why? Because it is the FASTEST among presented solutions :)

require(microbenchmark)
require(stringi)
require(stringr)
x <- c(letters,NA,paste(sample(letters,2000,TRUE),collapse=" "))
microbenchmark(nchar(x),str_length(x),stri_length(x))
Unit: microseconds
           expr    min     lq  median      uq     max neval
       nchar(x) 11.868 12.776 13.1590 13.6475  41.815   100
  str_length(x) 30.715 33.159 33.6825 34.1360 173.400   100
 stri_length(x)  2.653  3.281  4.0495  4.5380  19.966   100

and also works fine with NA's

nchar(NA)
## [1] 2
stri_length(NA)
## [1] NA



回答3:


nchar("STRING")

Check out this




回答4:


You could also use the stringr package:

library(stringr)
str_length("foo")
[1] 3



回答5:


The keepNA = TRUE option prevents problems with NA

nchar(NA)
## [1] 2
nchar(NA, keepNA=TRUE)
## [1] NA



回答6:


nchar(YOURSTRING)

you may need to convert to a character vector first;

nchar(as.character(YOURSTRING))


来源:https://stackoverflow.com/questions/11134812/how-to-find-the-length-of-a-string-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!