- 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 combine matrices having same number of columns in R?
The matrices that have same number of columns can be combined by rows. For example, if we have five matrices list, each having six columns then those matrices can be converted into a single matric by joining the rows of those matrices. It can be done by using do.call(rbind,”List_of_matrices_object_name”).
Example
Consider the below matrices and their list −
M1<-matrix(1:36,nrow=6) M1
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 7 13 19 25 31 [2,] 2 8 14 20 26 32 [3,] 3 9 15 21 27 33 [4,] 4 10 16 22 28 34 [5,] 5 11 17 23 29 35 [6,] 6 12 18 24 30 36
Example
M2<-matrix(sample(1:10,30,replace=TRUE),ncol=6) M2
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 6 5 5 5 4 6 [2,] 6 1 9 8 8 4 [3,] 10 3 9 3 7 10 [4,] 9 4 4 9 9 9 [5,] 4 9 6 6 5 5
Example
M3<-matrix(sample(1:50,30),ncol=6) M3
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 27 25 24 45 18 21 [2,] 22 46 9 34 26 44 [3,] 23 33 29 47 4 37 [4,] 41 31 39 50 19 15 [5,] 5 17 40 11 20 35
Example
M4<-matrix(rpois(36,5),ncol=6) M4
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 12 4 5 6 3 5 [2,] 6 6 1 9 3 6 [3,] 3 4 3 2 10 5 [4,] 1 6 8 9 6 7 [5,] 3 6 5 4 4 3 [6,] 8 4 1 7 9 4
Example
M5<-matrix(round(runif(36,2,5)),ncol=6) M5
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 2 4 4 3 4 4 [2,] 3 5 3 2 3 5 [3,] 2 5 2 4 4 2 [4,] 4 3 2 4 5 5 [5,] 3 3 3 3 3 3 [6,] 2 5 2 3 4 2
Example
List_M<-list(M1,M2,M3,M4,M5) List_M
Output
[[1]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 7 13 19 25 31 [2,] 2 8 14 20 26 32 [3,] 3 9 15 21 27 33 [4,] 4 10 16 22 28 34 [5,] 5 11 17 23 29 35 [6,] 6 12 18 24 30 36 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 6 5 5 5 4 6 [2,] 6 1 9 8 8 4 [3,] 10 3 9 3 7 10 [4,] 9 4 4 9 9 9 [5,] 4 9 6 6 5 5 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 27 25 24 45 18 21 [2,] 22 46 9 34 26 44 [3,] 23 33 29 47 4 37 [4,] 41 31 39 50 19 15 [5,] 5 17 40 11 20 35 [[4]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 2 4 2 3 5 2 [2,] 3 3 3 3 4 4 [3,] 4 4 3 4 2 3 [4,] 2 4 4 4 3 4 [5,] 4 5 5 3 2 3 [6,] 4 3 4 4 4 3 [[5]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 2 4 4 3 4 4 [2,] 3 5 3 2 3 5 [3,] 2 5 2 4 4 2 [4,] 4 3 2 4 5 5 [5,] 3 3 3 3 3 3 [6,] 2 5 2 3 4 2
Combining list of matrices into a single matrix by rows −
Example
do.call(rbind,List_M)
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 7 13 19 25 31 [2,] 2 8 14 20 26 32 [3,] 3 9 15 21 27 33 [4,] 4 10 16 22 28 34 [5,] 5 11 17 23 29 35 [6,] 6 12 18 24 30 36 [7,] 6 5 5 5 4 6 [8,] 6 1 9 8 8 4 [9,] 10 3 9 3 7 10 [10,] 9 4 4 9 9 9 [11,] 4 9 6 6 5 5 [12,] 27 25 24 45 18 21 [13,] 22 46 9 34 26 44 [14,] 23 33 29 47 4 \37 [15,] 41 31 39 50 19 15 [16,] 5 17 40 11 20 35 [17,] 2 4 2 3 5 2 [18,] 3 3 3 3 4 4 [19,] 4 4 3 4 2 3 [20,] 2 4 4 4 3 4 [21,] 4 5 5 3 2 3 [22,] 4 3 4 4 4 3 [23,] 2 4 4 3 4 4 [24,] 3 5 3 2 3 5 [25,] 2 5 2 4 4 2 [26,] 4 3 2 4 5 5 [27,] 3 3 3 3 3 3 [28,] 2 5 2 3 4 2
- Related Articles
- How to find the row variance of columns having same name in R matrix?
- How to find the row median of columns having same name in R matrix?
- How to find the row means of columns having same name in R matrix?
- How to find the row total of columns having same name in R matrix?
- How to find the row median of columns having same name in R data frame?
- How to find the row variance of columns having same name in R data frame?
- How to find the row mean of columns having same name in R data frame?
- How to find the row standard deviation of columns having same name in R matrix?
- How to find the row total of columns having same name in R data frame?
- How to add values in columns having same name and merge them in R?
- How to combine two matrices to create a block-diagonal matrix in R?
- How to create the combination of rows in two data frames having same columns in R?
- How to find the row median of columns having same name in data.table object in R?
- How to find the row variance of columns having same name in data.table object in R?
- How to find the row total of columns having same name in data.table object in R?

Advertisements