[R] Repeat elements of character vector

Chel Hee Lee chl948 at mail.usask.ca
Fri Jan 30 15:54:23 CET 2015


 > paste(rep(v1, each=3), 1:3, sep="_")
[1] "a_1" "a_2" "a_3" "b_1" "b_2" "b_3" "c_1" "c_2" "c_3"
 >

Is this what you are looking for?  I hope this helps.

Chel Hee Lee

On 1/30/2015 7:34 AM, Knut Hansen wrote:
> I have a vector of several character elements:
> v1 <- c("a", "b", "c")
>
> I want each of these elements to be repeated n times and the number of the
> repetition added as part of the element text, hence rep() will not work. For
> n=3 the result would be:
> v2 <- c("a_1", "a_2", "a_3", "b_1", "b_2", "b_3", "c_1", "c_2", "c_3")
>
> Knut Hansen
> knut.hansen at uit.no
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list