[R] Interleaving elements of two vectors?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Mar 6 07:45:39 CET 2006


You don't have y[4] ....  But if you did, as.vector(rbind(x, y))

On Mon, 6 Mar 2006, Ajay Narottam Shah wrote:

> Suppose one has
>
>        x <- c(1,  2,  7,  9,  14)
>        y <- c(71, 72, 77)
>
> How would one write an R function which alternates between elements of
> one vector and the next? In other words, one wants
>
>        z <- c(x[1], y[1], x[2], y[2], x[3], y[3], x[4], y[4], x[5], y[5])
>
> I couldn't think of a clever and general way to write this. I am aware
> of gdata::interleave() but it deals with interleaving rows of a data
> frame, not elems of vectors.
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list