[R] Duplicate rows of matrix

Bruno C. bruno.c at inwind.it
Fri Mar 9 18:17:04 CET 2007


Hello my problem is the following:

I have a matrix A and a vector B which contains as many rows as A.

I need to build a matrix C which contains B[i]-times the row A[i,] and this for each line of A.

if for example A is

    [1]    [2]
[1]  8      9.4
[2]  4.2    1.1

and B is (3,1). Then C will be:
    [1]    [2]
[1]  8      9.4
[2]  8      9.4
[3]  8      9.4
[4]  4.2    1.1


I have some working code which go through all the lines of A and for each line does a rbind(C, A[i,]) B[i]-times 
But this is quite time consuming given that each rbind rebuild a new matrix ... is there any faster way?
I can think of some minor improvements like building a matrix C of zeros, containing as many columns as A and as many columns as the sum of elements of B ... and the filing it.

But I was more looking for some already implemented function/package, is there any?

Thanx



------------------------------------------------------
Leggi GRATIS le tue mail con il telefonino i-mode™ di Wind
http://i-mode.wind.it



More information about the R-help mailing list