连接字符串/字符向量
如果我有一个字符类型的向量,如何将这些值连接成字符串?下面是我如何使用paste()来做到这一点:
sdata = c('a', 'b', 'c')
paste(sdata[1], sdata[2], sdata[3], sep ='')
产生“abc”
。
但当然,只有当我提前知道 sdata 的长度时,这才有效。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以将
stri_paste
函数与stringi
包中的collapse
参数一起使用,如下所示:以及一些基准测试:
You can use
stri_paste
function withcollapse
parameter fromstringi
package like this:And some benchmarks:
tidyverse
stringr
包有一些快速的方法可以实现这一点。str_flatten
默认情况下会折叠字符向量,不带空格,但也有
collapse
参数:还有一个可选的
last
参数用于最终分隔符的位置。str_c
与
paste
类似,您需要指定collapse
参数来完成此操作:str_flatten_comma
自
stringr
1.5.0 如果您想要逗号分隔折叠。这里的last
参数识别牛津逗号:stringfish(更快)
对于大多数情况
stringi
和stringr
将提供足够的速度,但如果你需要一些东西更快地考虑stringfish
包:Base R
为了完整性,您可以使用
paste0
,尽管这里比paste
没有明显的优势:基准测试
跨向量 大小为 10K、100K、1M 和 10M
stringfish
始终能获得更快的结果:基准代码
tidyverse
The
stringr
package has a few, fast ways you could accomplish this.str_flatten
By default will collapse your character vector with no spaces, but does have
collapse
argument as well:Also has an optional
last
argument to use in place of the final separator.str_c
Similar to
paste
with acollapse
argument you need to specify to accomplish this:str_flatten_comma
New as of
stringr
1.5.0 if you want a comma delimited collapse. Here thelast
argument recognizes the Oxford comma:stringfish (faster)
For most cases
stringi
andstringr
will provide enough speed, but if you need something faster consider thestringfish
package:Base R
For completeness, you could use
paste0
, though there is no obvious advantage here overpaste
:Benchmark
Across vector sizes of 10K, 100K, 1M and 10M
stringfish
yields consistently faster results:Benchmark Code
马特·特纳的答案绝对是正确的答案。然而,本着肯·威廉姆斯回答的精神,你也可以这样做:
Matt Turner's answer is definitely the right answer. However, in the spirit of Ken Williams' answer, you could also do:
对于
sdata
:对于整数向量:
For
sdata
:For a vector of integers:
另一种方法是使用glue包:
Another way would be to use
glue
package:这是一个小实用函数,可将命名或未命名的值列表折叠为单个字符串,以便于打印。它还会打印代码行本身。它来自我的 R 页面中的列表示例。
生成一些命名或未命名的列表:
这是将命名或未命名列表转换为字符串的函数:
使用之前创建的列表测试该函数:
使用列表元素的子集测试该函数:
Here is a little utility function that collapses a named or unnamed list of values to a single string for easier printing. It will also print the code line itself. It's from my list examples in R page.
Generate some lists named or unnamed:
Here is the a function that will convert named or unnamed list to string:
Testing the function with the lists created prior:
Testing the function with subset of list elements:
尝试在粘贴函数中使用空的collapse参数:
paste(sdata,collapse = '')
感谢http://twitter.com/onelinetips/status/7491806343
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: