How to create a matrix with equal rows in R?


If we have a single row for a matrix then creation of a matrix with equal rows can be easily done with the help of rep function and if we do not have the row then we would need to pass the row value inside rep function.

Check out the below examples to understand how to create a matrix with equal rows if one row is known.

Example 1

Consider the below vector −

Row_1<-rpois(5,5)

To create a matrix M1 using Row_1, use the code given below −

Row_1<-rpois(5,5)
M1<-matrix(rep(Row_1,20),ncol=5,byrow=TRUE)
M1

Output

If you execute all the above given snippets as a single program, it generates the following output −

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

Example 2

Consider the below vector −

Row_2<-rpois(5,1)

To create matrix M2 using Row_2, use the code given below −

Row_2<-rpois(5,1)
M2<-matrix(rep(Row_2,20),ncol=5,byrow=TRUE)
M2

Output

If you execute all the above given snippets as a single program, it generates the following output −

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

Example 3

Consider the below vector −

Row_3<-rnorm(3)

To create matrix M3 using Row_3, use the code given below −

Row_3<-rnorm(3)
M3<-matrix(rep(Row_3,20),ncol=3,byrow=TRUE)
M3

Output

If you execute all the above given snippets as a single program, it generates the following output −

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

Updated on: 11-Nov-2021

560 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements