如何连接两个字符串?
如何连接(合并、组合)两个值? 例如,我有:
tmp = cbind("GAD", "AB")
tmp
# [,1] [,2]
# [1,] "GAD" "AB"
我的目标是将“tmp”中的两个值连接到一个字符串:
tmp_new = "GAD,AB"
哪个函数可以为我执行此操作?
How can I concatenate (merge, combine) two values?
For example I have:
tmp = cbind("GAD", "AB")
tmp
# [,1] [,2]
# [1,] "GAD" "AB"
My goal is to concatenate the two values in "tmp" to one string:
tmp_new = "GAD,AB"
Which function can do this for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
是要走的路。正如前面的海报所指出的,粘贴可以做两件事:
将值连接成一个“字符串”,例如
,参数
sep
指定要连接的参数之间要使用的字符,或折叠字符向量
,其中参数
collapse
指定要折叠的向量元素之间使用的字符。您甚至可以将两者结合起来:
is the way to go. As the previous posters pointed out, paste can do two things:
concatenate values into one "string", e.g.
where the argument
sep
specifies the character(s) to be used between the arguments to concatenate,or collapse character vectors
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:
help.search()
是一个方便的函数,例如将引导您到
paste()
。help.search()
is a handy function, e.g.will lead you to
paste()
.对于第一个非
paste()
答案,我们可以查看stringr::str_c()
(然后查看下面的toString()
)。它的存在时间没有这个问题那么长,所以我认为提及它也存在是有用的。正如您所看到的,使用起来非常简单。
从它的文档文件描述来看,它很好地契合了这个问题。
添加于 2016 年 4 月 13 日:它与您想要的输出(额外的空间)不完全相同,但也没有人提到它。
toString()
基本上是paste()
的一个版本,其中collapse = ", "
是硬编码的,所以你可以这样做For the first non-
paste()
answer, we can look atstringr::str_c()
(and thentoString()
below). It hasn't been around as long as this question, so I think it's useful to mention that it also exists.Very simple to use, as you can see.
From its documentation file description, it fits this problem nicely.
Added 4/13/2016: It's not exactly the same as your desired output (extra space), but no one has mentioned it either.
toString()
is basically a version ofpaste()
withcollapse = ", "
hard-coded, so you can do正如其他人指出的那样,
paste()
是正确的选择。但每次您需要非默认分隔符时都必须输入paste(str1, str2, str3, sep='')
,这可能会很烦人。您可以非常轻松地创建包装函数,使生活变得更加简单。例如,如果您发现自己经常连接不带分隔符的字符串,您可以这样做:
或者如果您经常想要连接来自向量的字符串(例如 PHP 中的
implode()
):允许您这样做this:
此外,还有内置的
paste0
,它与我的implode
执行相同的操作,但不允许自定义分隔符。它比paste()
稍微高效一些。As others have pointed out,
paste()
is the way to go. But it can get annoying to have to typepaste(str1, str2, str3, sep='')
everytime you want the non-default separator.You can very easily create wrapper functions that make life much simpler. For instance, if you find yourself concatenating strings with no separator really often, you can do:
or if you often want to join strings from a vector (like
implode()
from PHP):Allows you do do this:
Also, there is the built-in
paste0
, which does the same thing as myimplode
, but without allowing custom separators. It's slightly more efficient thanpaste()
.我通过搜索 R 连接字符串从 Google 找到了这个:http://stat.ethz.ch/R-manual/R-patched/library/base/html/paste.html
I found this from Google by searching for R concatenate strings: http://stat.ethz.ch/R-manual/R-patched/library/base/html/paste.html
或者,如果您的目标是直接输出到文件或标准输出,则可以使用
cat
:Alternatively, if your objective is to output directly to a file or stdout, you can use
cat
:另一种方式:
它有时比
paste()
函数有用。%s
表示将包含主观字符串的位置。请注意,当您尝试构建路径时,这会派上用场:
输出
Another way:
It sometimes useful than
paste()
function.%s
denotes the place where the subjective strings will be included.Note that this will come in handy as you try to build a path:
output
您可以创建自己的运算符:
您还可以重新定义“and”(
&
)运算符:弄乱基线语法很丑陋,但使用
paste()/paste0()
也是如此code> 如果您只使用自己的代码,您可以(几乎总是)替换逻辑& and
运算符与*
进行逻辑值相乘,而不是使用逻辑“and &”You can create you own operator :
You can also redefine 'and' (
&
) operator :messing with baseline syntax is ugly, but so is using
paste()/paste0()
if you work only with your own code you can (almost always) replace logical& and
operator with*
and do multiplication of logical values instead of using logical 'and &'给定您创建的矩阵 tmp:
我认为您使用 cbind 创建矩阵是有某种原因的,而不是简单地:
Given the matrix, tmp, that you created:
I assume there is some reason why you're creating a matrix using cbind, as opposed to simply:
glue
是一个新函数、数据类和包,作为tidyverse
的一部分开发,具有许多扩展功能。它结合了 Paste、sprintf 和之前其他答案的功能。由 reprex 包 (v0.2.1) 于 2019 年 3 月 6 日创建
是的,对于这个问题中的简单例子来说,它有点矫枉过正,但在许多情况下都很强大。 (请参阅https://glue.tidyverse.org/)
与
粘贴 与下面的
with
。glue
代码更容易输入,看起来也更容易阅读。由 reprex 包 (v0.2.1) 创建于 2019 年 3 月 6 日
glue
is a new function, data class, and package that has been developed as part of thetidyverse
, with a lot of extended functionality. It combines features from paste, sprintf, and the previous other answers.Created on 2019-03-06 by the reprex package (v0.2.1)
Yes, it's overkill for the simple example in this question, but powerful for many situations. (see https://glue.tidyverse.org/)
Quick example compared to
paste
withwith
below. Theglue
code was a bit easier to type and looks a bit easier to read.Created on 2019-03-06 by the reprex package (v0.2.1)
考虑字符串是列且结果应该是新列的情况:
如果需要粘贴所有列,则可以选择跳过
[c("a", "b")]
子集。Consider the case where the strings are columns and the result should be a new column:
Optionally, skip the
[c("a", "b")]
subsetting if all columns needs to be pasted.另一个非粘贴答案:
在哪里
Another non-paste answer:
Where