[R] Interleaving elements of two vectors?
ggrothendieck at gmail.com
Mon Mar 6 06:12:17 CET 2006
Try this (note that your x and y do not have the same length
and in this case the expression will recycle the shorter one
and give a warning):
z <- c(rbind(x, y))
On 3/5/06, Ajay Narottam Shah <ajayshah at mayin.org> 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, y, x, y, x, y, x, y, x, y)
> 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.
> Ajay Shah http://www.mayin.org/ajayshah
> ajayshah at mayin.org http://ajayshahblog.blogspot.com
> <*(:-? - wizard who doesn't know the answer.
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help