如何连接两个字符串? [英] How can two strings be concatenated?
问题描述
如何连接(合并、组合)两个值?例如我有:
How can I concatenate (merge, combine) two values? For example I have:
tmp = cbind("GAD", "AB")
tmp
# [,1] [,2]
# [1,] "GAD" "AB"
我的目标是将tmp"中的两个值连接成一个字符串:
My goal is to concatenate the two values in "tmp" to one string:
tmp_new = "GAD,AB"
哪个函数可以为我做这件事?
Which function can do this for me?
推荐答案
paste()
是要走的路.正如之前的海报所指出的,粘贴可以做两件事:
is the way to go. As the previous posters pointed out, paste can do two things:
将值连接成一个字符串",例如
concatenate values into one "string", e.g.
> paste("Hello", "world", sep=" ")
[1] "Hello world"
其中参数 sep
指定要在要连接的参数之间使用的字符,或折叠字符向量
where the argument sep
specifies the character(s) to be used between the arguments to concatenate,
or collapse character vectors
> x <- c("Hello", "World")
> x
[1] "Hello" "World"
> paste(x, collapse="--")
[1] "Hello--World"
其中参数 collapse
指定要在要折叠的向量元素之间使用的字符.
where the argument collapse
specifies the character(s) to be used between the elements of the vector to be collapsed.
您甚至可以将两者结合起来:
You can even combine both:
> paste(x, "and some more", sep="|-|", collapse="--")
[1] "Hello|-|and some more--World|-|and some more"
这篇关于如何连接两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!