How to replicate a vector to create matrix in R?


The matrix can be created by using matrix function in R and if we want to create a matrix by replicating a vector then we just need to focus on the replication. For example, if we have a vector V and we want to create matrix by replicating V two times then the matrix can be created as matrix(replicate(2,V),nrow=2).

Example1

 Live Demo

V1<-1:5
matrix(replicate(2,V1),nrow=5)

Output

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

Example

matrix(replicate(5,V1),nrow=5)

Output

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

Example

matrix(replicate(10,V1),nrow=5)

Output

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

Example

matrix(replicate(9,V1),nrow=5)

Output

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

Example2

 Live Demo

V2<-rpois(10,5)
V2

Output

[1] 1 4 2 6 7 2 4 4 2 8

Example

matrix(replicate(2,V2),nrow=10)

Output

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

Example

matrix(replicate(5,V2),nrow=10)

Output

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

Example

matrix(replicate(10,V2),nrow=10)

Output

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

Example3

 Live Demo

V3<-sample(0:9,10,replace=TRUE)
V3

Output

[1] 1 8 8 5 9 7 0 7 1 8
matrix(replicate(5,V3),nrow=10)

Output

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

Example

matrix(replicate(3,V3),nrow=10)

Output

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

Example

matrix(replicate(12,V3),nrow=10)

Output

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

Updated on: 17-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements