如何将两个字符串串联在一起? [英] How can two strings be concatenated?

查看:314
本文介绍了如何将两个字符串串联在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何连接(合并,合并)两个值? 例如,我有:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆