- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replicate a matrix by rows in R?
The replication of matrix by rows means that repeating a matrix one or more times but row-wise. For example, if we have a matrix that contains only one row and three columns then the replication of that matrix three times will repeat that one row three times. This can be done by using rep function along with matrix function as shown in the below example.
Example
M<-matrix(1:25,ncol=5) M
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25
Replicating matrix M twice −
Example
matrix(rep(t(M),2),ncol=ncol(M),byrow=TRUE)
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 [6,] 1 6 11 16 21 [7,] 2 7 12 17 22 [8,] 3 8 13 18 23 [9,] 4 9 14 19 24 [10,] 5 10 15 20 25
Replicating matrix M thrice −
Example
matrix(rep(t(M),3),ncol=ncol(M),byrow=TRUE)
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 [6,] 1 6 11 16 21 [7,] 2 7 12 17 22 [8,] 3 8 13 18 23 [9,] 4 9 14 19 24 [10,] 5 10 15 20 25 [11,] 1 6 11 16 21 [12,] 2 7 12 17 22 [13,] 3 8 13 18 23 [14,] 4 9 14 19 24 [15,] 5 10 15 20 25
Replicating matrix M five times −
Example
matrix(rep(t(M),5),ncol=ncol(M),byrow=TRUE)
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 [6,] 1 6 11 16 21 [7,] 2 7 12 17 22 [8,] 3 8 13 18 23 [9,] 4 9 14 19 24 [10,] 5 10 15 20 25 [11,] 1 6 11 16 21 [12,] 2 7 12 17 22 [13,] 3 8 13 18 23 [14,] 4 9 14 19 24 [15,] 5 10 15 20 25 [16,] 1 6 11 16 21 [17,] 2 7 12 17 22 [18,] 3 8 13 18 23 [19,] 4 9 14 19 24 [20,] 5 10 15 20 25 [21,] 1 6 11 16 21 [22,] 2 7 12 17 22 [23,] 3 8 13 18 23 [24,] 4 9 14 19 24 [25,] 5 10 15 20 25
Replicating matrix M five times −
Example
matrix(rep(t(M),6),ncol=ncol(M),byrow=TRUE)
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 [6,] 1 6 11 16 21 [7,] 2 7 12 17 22 [8,] 3 8 13 18 23 [9,] 4 9 14 19 24 [10,] 5 10 15 20 25 [11,] 1 6 11 16 21 [12,] 2 7 12 17 22 [13,] 3 8 13 18 23 [14,] 4 9 14 19 24 [15,] 5 10 15 20 25 [16,] 1 6 11 16 21 [17,] 2 7 12 17 22 [18,] 3 8 13 18 23 [19,] 4 9 14 19 24 [20,] 5 10 15 20 25 [21,] 1 6 11 16 21 [22,] 2 7 12 17 22 [23,] 3 8 13 18 23 [24,] 4 9 14 19 24 [25,] 5 10 15 20 25 [26,] 1 6 11 16 21 [27,] 2 7 12 17 22 [28,] 3 8 13 18 23 [29,] 4 9 14 19 24 [30,] 5 10 15 20 25
- Related Articles
- How to replicate a vector to create matrix in R?
- How to divide matrix rows in R by row median?
- How to combine two rows in R matrix by addition?
- How to expand a matrix rows by their index position in R?
- How to divide the matrix rows by row minimum in R?
- How to divide the matrix rows by row maximum in R?
- How to divide matrix rows by number of columns in R?
- How to randomize rows of a matrix in R?
- How to divide the matrix rows by row standard deviation in R?
- How to combine matrix rows alternately in R?
- How to convert matrix rows into a list in R?
- How to create a matrix with equal rows in R?
- How to multiply a matrix columns and rows with the same matrix rows and columns in R?
- How to multiple a matrix rows in R with a vector?
- How to divide matrix rows by maximum value in each row excluding 0 in R?

Advertisements