Concatenating Matrices in R

RMatrixConcatenation

R Problem Overview


How can I concatenate matrices of same columns but different number of rows? For example, I want to concatenate a (dim(a) = 15 7000) and b (dim(b) = 16 7000) and I want the result to be a matrix of 31 rows by 7000 columns.

Can I also do this for matrices with a different number of rows and columns? Say I want to combine a matrix of 15 rows and 7000 columns with another of 16 rows and 7500 columns. Can I create one dataset with that?

R Solutions


Solution 1 - R

Sounds like you're looking for rbind:

> a<-matrix(nrow=10,ncol=5)
> b<-matrix(nrow=20,ncol=5)
> dim(rbind(a,b))
[1] 30  5

Similarly, cbind stacks the matrices horizontally.

I am not entirely sure what you mean by the last question ("Can I do this for matrices of different rows and columns.?")

Solution 2 - R

cbindX from the package gdata combines multiple columns of differing column and row lengths. Check out the page here:

http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/gdata/html/cbindX.html

It takes multiple comma separated matrices and data.frames as input :) You just need to

install.packages("gdata", dependencies=TRUE)

and then

library(gdata)
concat_data <- cbindX(df1, df2, df3) # or cbindX(matrix1, matrix2, matrix3, matrix4)

Solution 3 - R

Others have addressed the matter of concatenating two matrices:

  • horizontally with cbind (the "c" stands for "column", so you are binding the columns of the two matrices); or
  • vertically with rbind (the "r" stands for "row", so you are binding the rows of the two matrices).

What others haven't pointed out explicitly is that:

  • because cbind binds columns, the two matrices have to have the same number of rows: cbind builds a matrix that is wider, but it needs the "height" (# of rows) of the two matrices to match; and
  • because rbind binds rows, the two matrices have to have the same number of columns: rbind builds a matrix that is taller, but it needs the "width" (# of columns) of the two matrices to match.

Take a look at this:

> A <- matrix(nrow = 3, ncol = 4)
> B <- matrix(nrow = 3, ncol = 5)
> C <- matrix(nrow = 4, ncol = 5)
> D <- cbind(A, B)  # Works because A and B have same # of rows
> cbind(A, C)       # Fails
Error in cbind(A, C) : number of rows of matrices must match (see arg 2)
> E <- rbind(B, C)  # Works because B and C have same # of columns
> rbind(A, C)       # Fails
Error in rbind(A, C) : 
  number of columns of matrices must match (see arg 2)

So, no, you cannot put together two matrices if they have a different number of rows and a different number of columns. You would need to do something to either one of the matrices first, to make sure that their shapes become compatible for concatenation.

Solution 4 - R

From the dplyr documentation we have bind_cols and bind_rows:

bind_cols	Efficiently bind multiple data frames 
 by row and column
bind_rows	Efficiently bind multiple data frames 
 by row and column

So using dplyr:

A = matrix(1:9, ncol = 3)
B = matrix(1:9, ncol = 3)

A %>% as_tibble(A,B) %>% bind_rows(as_tibble(B))
     V1    V2    V3
  <int> <int> <int>
1     1     4     7
2     2     5     8
3     3     6     9
4     1     4     7
5     2     5     8
6     3     6     9

A %>% as_tibble() %>% bind_cols(as_tibble(B))
  V1...1 V2...2 V3...3 V1...4 V2...5 V3...6
   <int>  <int>  <int>  <int>  <int>  <int>
1      1      4      7      1      4      7
2      2      5      8      2      5      8
3      3      6      9      3      6      9

If you want return as a matrix just %>% as.matrix()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionConcerned_CitizenView Question on Stackoverflow
Solution 1 - RNPEView Answer on Stackoverflow
Solution 2 - RgawbulView Answer on Stackoverflow
Solution 3 - RRGSView Answer on Stackoverflow
Solution 4 - Rrubengavidia0xView Answer on Stackoverflow