[R] Help with use of rep function in R

David Winsemius dwinsemius at comcast.net
Tue Sep 8 05:49:18 CEST 2009


On Sep 7, 2009, at 10:20 PM, Subodh Acharya wrote:

> Dear List,I am trying to use rep function in the following conditions
> A = c( 5, 6, 7, 11, 9, 12, 10, 15)
> B = c(12,15, 21, 31, 25, 27,32, *34*,13,12, 34, 33, 24, 29, 26,
> *28*,22,14,27,22,21,12,32,
> 16)
>
> I need to repeat each element of A, as many times as each element of  
> B, for
> the entire length of B.
> for example,
> repeat 5, for 12 times, 6 for 15 times,........, 15 for 34 times,  
> and then,
> again, 5 for 13 times, 6 for 12 times,........, 15 for 28 times, and  
> so on.
>
> I used, the function
> rep(A, times = B)
> It didn't work because apparently, the times command , worked for  
> only if
> the length of A and B was equal.

unlist( mapply( rep, A, B))



David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list