How to create a matrix using vector generated with rep function in R?


Matrix can be only generated if we pass an even number of elements for it. If we want to create a matrix using vector generated with rep function then the length of this vector must be divisible by 2. For example, if we have a vector x that is created with rep function and it’s length is 20 then the matrix say M of size 10x2 using that vector can be constructed by using matrix(x,ncol=2).

Example 1

Live Demo

> x<-rep(rpois(20,5),2)
> M1<-matrix(x,ncol=2)
> M1

Output

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

Example 2

Live Demo

> x<-rep(rpois(20,5),2)
> M2<-matrix(x,ncol=4)
> M2

Output

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

Example 3

Live Demo

> x<-rep(rpois(20,5),2)
> M3<-matrix(x,ncol=5)
> M3

Output

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

Example 4

Live Demo

> y<-rep(rnorm(10,5,1),5)
> M4<-matrix(y,nrow=10)
> M4

Output

       [,1]     [,2]       [,3]     [,4]   [,5]
[1,] 6.239542 6.239542 6.239542 6.239542 6.239542
[2,] 7.033764 7.033764 7.033764 7.033764 7.033764
[3,] 3.970498 3.970498 3.970498 3.970498 3.970498
[4,] 4.273613 4.273613 4.273613 4.273613 4.273613
[5,] 6.090508 6.090508 6.090508 6.090508 6.090508
[6,] 3.803242 3.803242 3.803242 3.803242 3.803242
[7,] 6.272942 6.272942 6.272942 6.272942 6.272942
[8,] 6.160341 6.160341 6.160341 6.160341 6.160341
[9,] 2.255923 2.255923 2.255923 2.255923 2.255923
[10,] 5.000681 5.000681 5.000681 5.000681 5.000681

Example 5

Live Demo

> y<-rep(rnorm(10,5,1),5)
> M5<-matrix(y,nrow=25)
> M5

Output

       [,1]     [,2]
[1,] 6.239542 3.803242
[2,] 7.033764 6.272942
[3,] 3.970498 6.160341
[4,] 4.273613 2.255923
[5,] 6.090508 5.000681
[6,] 3.803242 6.239542
[7,] 6.272942 7.033764
[8,] 6.160341 3.970498
[9,] 2.255923 4.273613
[10,] 5.000681 6.090508
[11,] 6.239542 3.803242
[12,] 7.033764 6.272942
[13,] 3.970498 6.160341
[14,] 4.273613 2.255923
[15,] 6.090508 5.000681
[16,] 3.803242 6.239542
[17,] 6.272942 7.033764
[18,] 6.160341 3.970498
[19,] 2.255923 4.273613
[20,] 5.000681 6.090508
[21,] 6.239542 3.803242
[22,] 7.033764 6.272942
[23,] 3.970498 6.160341
[24,] 4.273613 2.255923
[25,] 6.090508 5.000681

Example 6

Live Demo

> y<-rep(rnorm(10,5,1),5)
> M6<-matrix(rep(c(1,5,10,15,20,25),5),nrow=10)
> M6

Output

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

Example 7

Live Demo

> y<-rep(rnorm(10,5,1),5)
> M7<-matrix(rep(c(1,5,10,15,20,25),5),nrow=5)
> M7

Output

   [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 25 20 15 10 5
[2,] 5 1 25 20 15 10
[3,] 10 5 1 25 20 15
[4,] 15 10 5 1 25 20
[5,] 20 15 10 5 1 25

Example 8

Live Demo

> y<-rep(rnorm(10,5,1),5)
> M8<-matrix(rep(c(1,5,10,15,20,25),5),nrow=15)
> M8

Output

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

Updated on: 19-Nov-2020

260 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements