Concatenate a vector of strings/character

If I have a vector of type character, how can I concatenate the values into string? Here's how I would do it with paste():

sdata = c('a', 'b', 'c')
paste(sdata[1], sdata[2], sdata[3], sep ='')

yielding "abc" .

But of course, that only works if I know the length of sdata ahead of time.


Try using an empty collapse argument within the paste function:

paste(sdata, collapse = '')

Thanks to http://twitter.com/onelinetips/status/7491806343


Matt's answer is definitely the right answer. However, here's an alternative solution for comic relief purposes:

do.call(paste, c(as.list(sdata), sep = ""))

For sdata :

gsub(", ","",toString(sdata))

For a vector of integers:

gsub(", ","",toString(c(1:10)))
链接地址: http://www.djcxy.com/p/24858.html

上一篇: 2个字符串如何连接?

下一篇: 连接字符串/字符的向量