• R语言
  • 如何连接一个字符向量的所有元素为一个字符串?

a=c('11','22','33')

要得到一个字符串 112233

要用什么函数?



谢谢
嗯,toString()倒也是一种办法
4 个月 后
toString()不行的吧,试了一下只能得到"11, 22, 33"

还是用paste吧,要不就把toString改改,既然都是用paste实现的嘛!



参考:

toString <- function(x, ...) UseMethod("toString")

toString.default <- function(x, width = NULL, ...)

{

  string <- paste(x, collapse=", ")

  if( missing(width) || is.null(width) || width == 0) return(string)

  if( width < 0 ) stop("'width' must be positive")

  if(nchar(string, type = "w") > width) {

    width <- max(6, width) ## Leave something!

    string <- paste(strtrim(string, width - 4), "....", sep = "")

  }

  string

}
18 天 后
3 年 后

回复 第7楼 的 谢益辉:太有用了。我本来还疑惑a的长度不知道怎么办呢。