[R] Odp: Manual recycling of vectors?

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Fri Oct 19 16:40:04 CEST 2007


Petr PIKAL wrote:
> Hi
>
> r-help-bounces at r-project.org napsal dne 19.10.2007 15:58:43:
>
>   
>> Sorry if this is already answered somewhere, but I could not find it. 
>> I have two vectors, x,y, of different length, and I want to recycle 
>> the smaller one (whichever one it is) until they have the same 
>> length. I was wondering if there is a anything better than something 
>> like:
>>
>> x<-1:3
>> y<-1:10
>> x<-rep(x,length=max(x,y))
>> y<-rep(y,length=max(x,y))
>>     
>
> E.g.
>
> mat<-cbind(x,y)
>
> gives you desired recycling in matrix form or
>
> x*(y>0)
>
> if you know that y is longer than x and all number are bigger than 0.
>   
It doesn't really help, though. I'd go for the straightforward

lx <- length(x)
ly <- length(y)
if (lx < ly)
   x <- rep(x, length=ly)
else if (lx > ly)
   y <- rep(y, length=lx)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list