Skip to content

rbind for matrices - rep argument

8 messages · Niccolò Bassani, Dimitris Rizopoulos, Henrique Dallazuanna +5 more

#
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
Niccol? Bassani wrote:

  
    
#
On Wed, 2009-01-07 at 16:22 +0100, Niccol? Bassani wrote:
Hello,

If your matrix is

kk <- matrix( 1:16, 4, 4)

You can do

kkk <- lapply( 1:5, function(x) kk )
do.call(rbind, kkk)

You can write your code in a single line, though. I used 5 here as a
matter of example. You can build a function on these lines with an
arbitrary argument if need be.

Carlos J. Gil Bellosta
http://www.datanalytics.com
#
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))
#
On 07-Jan-09 15:22:57, Niccol? Bassani wrote:
I don't know whether there is anywhere a ready-made function which
will implement a "rep" paramater for an rbind, but the following ad-hoc
function will do it for you efficiently (i.e. with the minimum number
of applications of the rbind() function).

To produce a result which consists of k replicates of x, row-bound:


  Krbind <- function(x,k){
    y <- x
    if(k==1) return(x)
    p <- floor(log2(k))
    for(i in (1:p)){
      z <- rbind(y,y)
      y <- z
    }
    k <- (k - 2^p)
    if(k==0) return(y) else return(rbind(y,Krbind(x,k)))
  }

## Example:

  Xdf <- data.frame(X1=c(1.1,1.2),X2=c(2.1,2.2),
                    X3=c(3.1,3.2),X4=c(4.1,4.2))

  Krbind(Xdf,6)
#     X1  X2  X3  X4
# 1  1.1 2.1 3.1 4.1
# 2  1.2 2.2 3.2 4.2
# 3  1.1 2.1 3.1 4.1
# 4  1.2 2.2 3.2 4.2
# 5  1.1 2.1 3.1 4.1
# 6  1.2 2.2 3.2 4.2
# 7  1.1 2.1 3.1 4.1
# 8  1.2 2.2 3.2 4.2
# 9  1.1 2.1 3.1 4.1
# 10 1.2 2.2 3.2 4.2
# 11 1.1 2.1 3.1 4.1
# 12 1.2 2.2 3.2 4.2

Of course, if you're not worried by efficiency, then the simple loop

  y <- x
  for(i in (1:(k-1))){y <- rbind(y,x)}

will do it!

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 07-Jan-09                                       Time: 18:08:14
------------------------------ XFMail ------------------------------
#
Ben Bolker wrote:
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")
#
For matrices you can use kronecker:
[,1] [,2] [,3] [,4]
 [1,]  1.1  2.1  3.1  4.1
 [2,]  1.2  2.2  3.2  4.2
 [3,]  1.1  2.1  3.1  4.1
 [4,]  1.2  2.2  3.2  4.2
 [5,]  1.1  2.1  3.1  4.1
 [6,]  1.2  2.2  3.2  4.2
 [7,]  1.1  2.1  3.1  4.1
 [8,]  1.2  2.2  3.2  4.2
 [9,]  1.1  2.1  3.1  4.1
[10,]  1.2  2.2  3.2  4.2
[11,]  1.1  2.1  3.1  4.1
[12,]  1.2  2.2  3.2  4.2


On Wed, Jan 7, 2009 at 1:08 PM, Ted Harding
<Ted.Harding at manchester.ac.uk> wrote: