如何在R中重复字符串N次? [英] How to repeat a String N times in R?
本文介绍了如何在R中重复字符串N次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Ruby 中,我可以使用以下内容重复字符串 n 次:
In Ruby I could repeat a String n times with the following:
E.G.<代码>"my_string" * 2 ->"my_stringmy_string"
在 R 中有没有同样简单的方法可以做到这一点?
Is there an equally simple way for doing this in R?
推荐答案
您可以使用 replicate
或 rep
:
You can use replicate
or rep
:
replicate(2, "my_string")
# [1] "my_string" "my_string"
rep("my_string", 2)
# [1] "my_string" "my_string"
paste
会把它放在一起:
paste(replicate(2, "my_string"), collapse = "")
# [1] "my_stringmy_string"
这篇关于如何在R中重复字符串N次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文