[R] a question on matrix manipulation

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Wed Jun 17 08:01:50 CEST 2009


Hi Lei,

one way to do this is the following:

mat <- matrix(rnorm(4*6), 4, 6)

ind <- c(3, 2, 4, 5)
mat[rep(seq_along(ind), ind), ]


I hope it helps.

Best,
Dimitris


Lei Liu wrote:
> Hi there,
> 
> I have a question on manipulating a matrix. Say I have a matrix A with 3 
> rows. I want to generate a new matrix B with 3 duplicates of the first 
> row of A, 2 duplicates of the second row, and 4 duplicates of the third 
> row. So B is a matrix with 9 rows. Or more general, I want to generate 
> (3, 2, 4, 5) duplicates of rows 1-4 of a matrix with 4 rows. Is there a 
> simple function to do it? Thanks a lot!
> 
> Lei
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list