本文介绍了paste/paste0 和 str_c 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎没有看到 paste/paste0str_c 之间的区别,用于将单个向量组合成单个字符串,多个将字符串合并为一个字符串,或将多个向量合并为一个字符串.

I don't seem to see a difference between paste/paste0 and str_c for combining a single vector into a single string, multiple strings into one string, or multiple vectors into a single string.

当我在写这个问题时,我发现了这个:https://www.rdocumentation.org/packages/stringr/versions/1.3.1/topics/str_c.来自[email protected] 的社区示例表示,不同之处在于str_c 将空白视为空白(而不是NA)并且更恰当地进行回收.还有其他区别吗?

While I was writing the question I found this: https://www.rdocumentation.org/packages/stringr/versions/1.3.1/topics/str_c. The community example from [email protected] says the difference is is that str_c treats blanks as blanks (not as NAs) and recycles more appropriately. Any other differences?

推荐答案

paste0(..., collapse = NULL)paste(..., sep = "", collapse = NULL),表示没有分隔符.换句话说,使用 paste0() 您不能应用某种分隔符,而使用 paste() 确实有该选项,而单个空格是默认值.

paste0(..., collapse = NULL)is a wrapper for paste(..., sep = "", collapse = NULL), which means there is no separator. In other words, with paste0() you can not apply some sort of separator, while you do have that option with paste(), whereas a single space is the default.

str_c(..., sep = "", collapse = NULL) 等价于 paste(),这意味着你可以选择自定义你想要的分隔器.不同之处在于 str_c() 默认没有分隔符,所以它的行为就像 paste0() 作为默认值.

str_c(..., sep = "", collapse = NULL) is equivalent to paste(), which means you do have the option to customize your desired separator. The difference is for str_c() the default is no separator, so it acts just like paste0() as a default.

Paste()paste0() 都是来自基础包的函数,而 str_c() 来自 stringr 包.

Paste() and paste0() are both functions from the base package, whereas str_c() comes from the stringr package.

我没有对其进行测试/微基准测试,但根据我的经验,我同意 Ryan str_c() 通常更快.

I did not test/microbenchmark it, but from my experience I do agree to Ryan str_c() is generally faster.

这篇关于paste/paste0 和 str_c 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 10:29