admin管理员组

文章数量:1410737

Problem

Say I have this matrix in R:

(x <- matrix(1:6, ncol = 3, byrow = TRUE))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

which I want to combine by rows multiple times. Here's an example using rbind()

rbind(x, x)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6

The problem is that I want to do this an indefinite number of times inside a function, and I'm trying to avoid just looping around x making copies of it.

What I've tried so far

I've tried several operations involving replicate(), array(), matrix(), *apply(), and the closest I get to what I want is this (for two reps):

replicate(2, rbind(x))
, , 1

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

, , 2

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

If I try to collapse this into a matrix, the elements get mixed up because of how the array sequence is stored:

as.vector(replicate(2, rbind(x)))
[1] 1 4 2 5 3 6 1 4 2 5 3 6

A clumsy solution

So far, the only way I got what I wanted is by abusing t():

t(array(t(x), dim = c(ncol(x), nrow(x) * 2)))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6

I bet there's a cleaner way to achieve this, but after a couple of hours I'm stumped. Any help?

Related questions

Here are some related questions with solutions I've tried without success:

  • Replicate Matrix in R
  • how to bind the same vector multiple times?
  • under what circumstances does R recycle?

Problem

Say I have this matrix in R:

(x <- matrix(1:6, ncol = 3, byrow = TRUE))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

which I want to combine by rows multiple times. Here's an example using rbind()

rbind(x, x)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6

The problem is that I want to do this an indefinite number of times inside a function, and I'm trying to avoid just looping around x making copies of it.

What I've tried so far

I've tried several operations involving replicate(), array(), matrix(), *apply(), and the closest I get to what I want is this (for two reps):

replicate(2, rbind(x))
, , 1

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

, , 2

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

If I try to collapse this into a matrix, the elements get mixed up because of how the array sequence is stored:

as.vector(replicate(2, rbind(x)))
[1] 1 4 2 5 3 6 1 4 2 5 3 6

A clumsy solution

So far, the only way I got what I wanted is by abusing t():

t(array(t(x), dim = c(ncol(x), nrow(x) * 2)))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6

I bet there's a cleaner way to achieve this, but after a couple of hours I'm stumped. Any help?

Related questions

Here are some related questions with solutions I've tried without success:

  • Replicate Matrix in R
  • how to bind the same vector multiple times?
  • under what circumstances does R recycle?
Share Improve this question asked Mar 7 at 8:07 Waldir LeoncioWaldir Leoncio 11.4k20 gold badges84 silver badges110 bronze badges 1
  • Duplicate of R duplicate a matrix several times and then bind by rows together – Gusbourne Commented Mar 8 at 9:48
Add a comment  | 

3 Answers 3

Reset to default 5

You could try

  • replicate with option simplify = FALSE, and use do.call(rbind,...) in turn
> do.call(rbind, replicate(2, x, FALSE))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6
  • Using kronecker
> kronecker(rep(1, 2), x)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6
  • Using aperm
> matrix(aperm(replicate(2, x), c(1, 3, 2)), ncol = ncol(x))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6
  • Using rep
> matrix(rep(t(x), 2), ncol = ncol(x), byrow = TRUE)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6

Using matrix with t and the appropriate arguments:

matrix(t(x), nrow=2 * nrow(x), ncol=ncol(x), byrow = TRUE)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6

repeat row indices.

> x[rep.int(1:2, 2), ]  ## two times
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6
> x[rep.int(seq_len(nrow(x)), 3), ]  ## three times, and not hard-coded
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6
[5,]    1    2    3
[6,]    4    5    6
> x[rep.int(seq_len(nrow(x)), 3)[seq_len(5)], ]  ## control the length
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    1    2    3
[4,]    4    5    6
[5,]    1    2    3

本文标签: rReplicate matrixStack Overflow