[R] rbind for matrices - rep argument

Patrick Burns pburns at pburns.seanet.com
Wed Jan 7 19:16:05 CET 2009


Ben Bolker wrote:
> Dimitris Rizopoulos <d.rizopoulos <at> erasmusmc.nl> writes:
>
>   
>> you can use indexing, e.g.,
>>
>> mat <- matrix(rnorm(20), 4, 5)
>>
>> mat
>> mat[rep(1:nrow(mat), 3), ]
>>
>> I hope it helps.
>>
>> Best,
>> Dimitris
>>     
>
>   or matrix trickery:
>
> z <- as.data.frame(matrix(sample(1:9,20,replace=TRUE),nrow=4))
> matrix(rep(as.matrix(z),3),ncol=ncol(z),byrow=TRUE)
>
>   I do sometimes wish there were a more generic Rep() defined as
>
> Rep <- function(x,n) {
>   result <- list()
>   for (i in 1:n) {
>      result[[i]] <- x
>   }
>   result
> }
>
> then one could say do.call("rbind",Rep(x,3))
>   

You mean like:

do.call('rbind', rep(list(x), 3))

Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
> ______________________________________________
> 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.
>
>
>




More information about the R-help mailing list