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.
--
Ajay Shah http://www.mayin.org/ajayshah
ajayshah at mayin.org
http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.
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[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. > > -- > 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 > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
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
For a general solution without warnings try
interleave <- function(v1,v2)
{
ord1 <- 2*(1:length(v1))-1
ord2 <- 2*(1:length(v2))
c(v1,v2)[order(c(ord1,ord2))]
}
interleave(rep(1,5),rep(3,8))
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gabor
> Grothendieck
> Sent: Monday, March 06, 2006 12:12 AM
> To: Ajay Narottam Shah
> Cc: R-help
> Subject: Re: [R] Interleaving elements of two vectors?
>
> 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[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.
> >
> > --
> > 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
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>