Dear all, I have got a question for generating repeats of a vector's elements. Please don't hesitate to email me back and say that it is very easy although I can't find a method to solve it. If I have a n-vector b, whose elements are b[1], b[2], ..., b[n], how can I generate such a vector b[1], b[1], b[2], b[1], b[2], b[3], b[1], b[2], b[3], b[4], ......, b[1], b[2], ..., b[n] which contains n(n+1)/2 elements. Thanks very much. Regards Chaofeng Kou [[alternative HTML version deleted]]
I would do it like this but I'm curious to see whether it can be easier solved :-) b <- c(1:10) vector_list <- lapply(1:length(b), FUN = function(x) { b[1:x] })) (for everything in a large vector just apply "unlist" to the "vector_list") Antje Chaofeng Kou schrieb:> Dear all, > > I have got a question for generating repeats of a vector's elements. > Please don't hesitate to email me back and say that it is very easy > although I can't find a method to solve it. > > If I have a n-vector b, whose elements are b[1], b[2], ..., b[n], how > can I generate such a vector > > b[1], b[1], b[2], b[1], b[2], b[3], b[1], b[2], b[3], b[4], > ......, b[1], b[2], ..., b[n] > > which contains n(n+1)/2 elements. > > Thanks very much. > > Regards > Chaofeng Kou > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
try this: b <- rnorm(4) ind <- unlist(lapply(seq_along(b), seq_len)) b b[ind] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://perswww.kuleuven.be/dimitris_rizopoulos/ ----- Original Message ----- From: "Chaofeng Kou" <kouchaofeng at gmail.com> To: <r-help at r-project.org> Sent: Friday, July 18, 2008 2:21 PM Subject: [R] generate repeats of a vector's elements> Dear all, > > I have got a question for generating repeats of a vector's elements. > Please don't hesitate to email me back and say that it is very easy > although I can't find a method to solve it. > > If I have a n-vector b, whose elements are b[1], b[2], ..., b[n], > how > can I generate such a vector > > b[1], b[1], b[2], b[1], b[2], b[3], b[1], b[2], b[3], b[4], > ......, b[1], b[2], ..., b[n] > > which contains n(n+1)/2 elements. > > Thanks very much. > > Regards > Chaofeng Kou > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm