Skip to content

Vector manipulation

4 messages · syrvn, Dimitris Rizopoulos, Jorge I Velez

#
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: