How to create duplicate matrices and merge them together in R?


To create duplicate matrices, we can use replicate function that will repeat the original matrix and if we want to merge those matrices together then we can use rbind with do.call. For example, if we have a matrix called M then creation of it’s one duplicate and merging them together can be done using the command −

do.call(rbind,replicate(2,M,simplify=FALSE))

Example

 Live Demo

M<-matrix(rpois(25,5),ncol=5)
M

Output

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

do.call(rbind,replicate(2,M,simplify=FALSE))

    [,1] [,2] [,3] [,4] [,5]
[1,]  3   5    6     8   7
[2,]  3   8    6     4   6
[3,]  8   5    5     4   7
[4,]  7   6    5     7   4
[5,]  3   3    4     4   3
[6,]  3   5    6     8   7
[7,]  3   8    6     4   6
[8,]  8   5    5     4   7
[9,]  7   6    5     7   4
[10,] 3   3    4     4   3

do.call(rbind,replicate(3,M,simplify=FALSE))

    [,1] [,2] [,3] [,4] [,5]
[1,]  3   5    6    8    7
[2,]  3   8    6    4    6
[3,]  8   5    5    4    7
[4,]  7   6    5    7    4
[5,]  3   3    4    4    3
[6,]  3   5    6    8    7
[7,]  3   8    6    4    6
[8,]  8   5    5    4    7
[9,]  7   6    5    7    4
[10,] 3   3    4    4    3
[11,] 3   5    6    8    7
[12,] 3   8    6    4    6
[13,] 8   5    5    4    7
[14,] 7   6    5    7    4
[15,] 3   3    4    4    3

do.call(rbind,replicate(4,M,simplify=FALSE))

    [,1] [,2] [,3] [,4] [,5]
[1,]  3   5    6    8    7
[2,]  3   8    6    4    6
[3,]  8   5    5    4    7
[4,]  7   6    5    7    4
[5,]  3   3    4    4    3
[6,]  3   5    6    8    7
[7,]  3   8    6    4    6
[8,]  8   5    5    4    7
[9,]  7   6    5    7    4
[10,] 3   3    4    4    3
[11,] 3   5    6    8    7
[12,] 3   8    6    4    6
[13,] 8   5    5    4    7
[14,] 7   6    5    7    4
[15,] 3   3    4    4    3
[16,] 3   5    6    8    7
[17,] 3   8    6    4    6
[18,] 8   5    5    4    7
[19,] 7   6    5    7    4
[20,] 3   3    4    4    3

do.call(rbind,replicate(6,M,simplify=FALSE))

    [,1] [,2] [,3] [,4] [,5]
[1,]  3   5    6    8    7
[2,]  3   8    6    4    6
[3,]  8   5    5    4    7
[4,]  7   6    5    7    4
[5,]  3   3    4    4    3
[6,]  3   5    6    8    7
[7,]  3   8    6    4    6
[8,]  8   5    5    4    7
[9,]  7   6    5    7    4
[10,] 3   3    4    4    3
[11,] 3   5    6    8    7
[12,] 3   8    6    4    6
[13,] 8   5    5    4    7
[14,] 7   6    5    7    4
[15,] 3   3    4    4    3
[16,] 3   5    6    8    7
[17,] 3   8    6    4    6
[18,] 8   5    5    4    7
[19,] 7   6    5    7    4
[20,] 3   3    4    4    3
[21,] 3   5    6    8    7
[22,] 3   8    6    4    6
[23,] 8   5    5    4    7
[24,] 7   6    5    7    4
[25,] 3   3    4    4    3
[26,] 3   5    6    8    7
[27,] 3   8    6    4    6
[28,] 8   5    5    4    7
[29,] 7   6    5    7    4
[30,] 3   3    4    4    3

Updated on: 06-Mar-2021

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements