Hello, I am stuck with the following problem. Consider the vector: vec <- c(2,4,6,9,10) I now want to use R to manipulate the vector as follows: [1] 2, 4, 2, 6, 2, 9, 2, 10 In words, the first element of the vector should be placed in front of each following number. Which R commands do I need to achieve that? Cheers -- View this message in context: http://r.789695.n4.nabble.com/Vector-manipulation-tp4381586p4381586.html Sent from the R help mailing list archive at Nabble.com.
One way is: vec <- c(2,4,6,9,10) c(rbind(vec[1], vec[-1])) I hope it helps. Best, Dimitris On 2/12/2012 6:54 PM, syrvn wrote:> Hello, > > > I am stuck with the following problem. Consider the vector: > > vec<- c(2,4,6,9,10) > > I now want to use R to manipulate the vector as follows: > > [1] 2, 4, 2, 6, 2, 9, 2, 10 > > In words, the first element of the vector should be placed in front of each > following number. > > Which R commands do I need to achieve that? > > > Cheers > > -- > View this message in context: http://r.789695.n4.nabble.com/Vector-manipulation-tp4381586p4381586.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/
Hi mentor_, Try c(sapply(vec[-1], function(x) c(vec[1], x))) # [1] 2 4 2 6 2 9 2 10 HTH, Jorge.- On Sun, Feb 12, 2012 at 12:54 PM, syrvn <> wrote:> Hello, > > > I am stuck with the following problem. Consider the vector: > > vec <- c(2,4,6,9,10) > > I now want to use R to manipulate the vector as follows: > > [1] 2, 4, 2, 6, 2, 9, 2, 10 > > In words, the first element of the vector should be placed in front of each > following number. > > Which R commands do I need to achieve that? > > > Cheers > > -- > View this message in context: > http://r.789695.n4.nabble.com/Vector-manipulation-tp4381586p4381586.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]