使用stringi程序包中的stri_dup函数
stri_dup("abc",3)
[1] "abcabcabc"
它也是矢量化的,因此您可以执行以下操作:
> stri_dup(letters[1:3],4)
[1] "aaaa" "bbbb" "cccc"
性能比较:)
> microbenchmark(stri_dup("abc",3),paste(replicate(3, "abc"), collapse = ""))
Unit: microseconds
expr min lq median uq max neval
stri_dup("abc", 3) 2.362 3.456 7.0030 7.853 64.071 100
paste(replicate(3, "abc"), collapse = "") 57.131 61.998 65.2165 68.017 200.626 100
> microbenchmark(stri_dup("abc",300),paste(replicate(300, "abc"), collapse = ""))
Unit: microseconds
expr min lq median uq max neval
stri_dup("abc", 300) 6.441 7.6995 10.2990 13.757 45.784 100
paste(replicate(300, "abc"), collapse = "") 390.137 419.7740 440.5345 460.042 573.975 100