str_dup

Duplicates a string a designated number of times.

Tidyverse reference page

Duplicate a string

Duplicate the string “hat” 3 times.

stringr::str_dup("hat", 3)
[1] "hathathat"

A separator can be included.

stringr::str_dup("lime", 4, sep = " ")
[1] "lime lime lime lime"

This can be carried out with a vector.

#Create a vector
vec <- c("rose", "thistle", "leek", "shamrock")
#Duplicate
vec |> stringr::str_dup(2, sep = ",")
[1] "rose,rose"         "thistle,thistle"   "leek,leek"        
[4] "shamrock,shamrock"

With a vector you can duplicate the different elements to differing amounts.

#Create a vector
vec <- c("once", "twice", "thrice")
#Duplicate
vec |> stringr::str_dup(1:3, sep = "|")
[1] "once"                 "twice|twice"          "thrice|thrice|thrice"