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?
- Duplicate of R duplicate a matrix several times and then bind by rows together – Gusbourne Commented Mar 8 at 9:48
3 Answers
Reset to default 5You could try
replicate
with optionsimplify = FALSE
, and usedo.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
rep
eat 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
版权声明:本文标题:r - Replicate matrix - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744942626a2633582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论