[R] repeated vector in matrix

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Jun 2 14:23:31 CEST 2005


"Friedrich, Andreas (dit)" <Andreas.Friedrich at dit.de> writes:

> Hallo,
> 
> I'll need a matrix with n rows of the an identical vector.
> 
> 
> 
> > h
> [1] 3 3 3 3 2 2 2
> 
> 
> The nmatrix should look like this:
> 
> > x<-rbind(h,h,h)
> > x
>   [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> h    3    3    3    3    2    2    2
> h    3    3    3    3    2    2    2
> h    3    3    3    3    2    2    2
> 
> 
> 
> but I need n rows which must be variable. Can anyone help me?

Like this, for instance:

> h<-c(3, 3, 3, 3, 2, 2, 2)
> n<-5
> matrix(h, nrow=n, ncol=length(h), byrow=TRUE)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    3    3    3    3    2    2    2
[2,]    3    3    3    3    2    2    2
[3,]    3    3    3    3    2    2    2
[4,]    3    3    3    3    2    2    2
[5,]    3    3    3    3    2    2    2


-- 
   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




More information about the R-help mailing list