[R] Repeat elements of character vector

Ivan Calandra ivan.calandra at univ-reims.fr
Fri Jan 30 15:57:23 CET 2015


Hi,

Take a look at the argument "each" from rep().
You could do that (there might be something shorter):

paste(rep(v1,each=3), 1:3, sep="_")

HTH,
Ivan

--
Ivan Calandra, ATER
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
ivan.calandra at univ-reims.fr
https://www.researchgate.net/profile/Ivan_Calandra

Le 30/01/15 14:34, Knut Hansen a écrit :
> 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