[R] alternative to rep

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Nov 13 23:43:22 CET 2002


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))).] 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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