[R] How to repeat vectors ?

Adrian DUSA adi at roda.ro
Sat Sep 30 13:48:54 CEST 2006


Maybe this one?

> MyMatrix <- matrix(1:4, nrow=2)

> MyMatrix
     [,1] [,2]
[1,]    1    3
[2,]    2    4

> MyMatrix[rep(seq(nrow(MyMatrix)), each=2), ]
     [,1] [,2]
[1,]    1    3
[2,]    1    3
[3,]    2    4
[4,]    2    4


HTH,
Adrian

On Saturday 30 September 2006 09:33, Tong Wang wrote:
> I just figured out a way to do this:
>           rep.vec <- function(X,n)   
> return(t(array(rep(X,n),c(length(X),n))))
>
>    Then,    apply(MyMatrix, 2, rep.vec,2)
>
> Is there a better way ?  Is there an internal function to repeat a vector
> or matrix ?
>
> Thanks a lot.
>
>
> ----- Original Message -----
> From: Tong Wang <wangtong at usc.edu>
> Date: Friday, September 29, 2006 11:23 pm
> Subject: How to repeat vectors ?
> To: r-help at stat.math.ethz.ch
>
> > Hi,
> >    If I have a matrix  , say       a11   a12
> >                                                   a21  a22
> >    Is there a routine to get:      a11  a12
> >                                                     a11  a12
> >                                                     a21   a22
> >                                                     a21   a22
> >
> >     Thanks a lot for any help.
> >
> > best

-- 
Adrian DUSA
Arhiva Romana de Date Sociale
Bd. Schitu Magureanu nr.1
050025 Bucuresti sectorul 5
Romania
Tel./Fax: +40 21 3126618 \
          +40 21 3120210 / int.101



More information about the R-help mailing list