Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

How to repeat a String N times in R?

In Ruby I could repeat a String n times with the following:

E.G. "my_string" * 2 -> "my_stringmy_string"

Is there an equally simple way for doing this in R?

question from:https://stackoverflow.com/questions/65845240/how-to-create-a-vector-a-by-sampling-that-contains-a-character-repeated-n-time

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 will put it together:

paste(replicate(2, "my_string"), collapse = "")
# [1] "my_stringmy_string"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...