Skip to content
Prev 166236 / 398502 Next

rbind for matrices - rep argument

Dimitris Rizopoulos <d.rizopoulos <at> erasmusmc.nl> writes:
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))