[R] alternative to rep

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Thu Nov 14 08:09:49 CET 2002


rep(x, each=) was something from S4 I added to R for 1.5.0 so it has
only been there for 6 months or so.

On 13 Nov 2002, Peter Dalgaard BSA wrote:

> Bill Shipley <Bill.Shipley at Usherbrooke.ca> writes:
>
> > Hello.  the rep(x) function replicates the vector x.  I am looking for
> > a quick way of "expanding" a vector.  Here is what I mean by "expand":
> >
> > given: x<-c(2,5,4)
> > If I "expand" x three times, then I will get 2,2,2,5,5,5,4,4,4.  In
> > other words, I want to keep the original ordering (2,5,4) but
> > duplicate each of the original entries three times.
> >
> > Is there such a function?  If not, can someone suggest a fast way of
> > doing this?
>
> It's easier than you think:
>
> > x <- c(2,5,4)
> > rep(x,each=3)
> [1] 2 2 2 5 5 5 4 4 4
>
> [That one escaped me until recently, too. I used to use
> rep(x,rep(3,length(x))).]

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list