Combine a List of Matrices to a Single Matrix by Rows

Combine a list of matrices to a single matrix by rows

Use do.call(rbind,...)

> m1 <- matrix(1, nrow=2, ncol=3)
> m2 <- matrix(2, nrow=3, ncol=3)
> l <- list(m1, m2)
> do.call(rbind, l)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
[3,] 2 2 2
[4,] 2 2 2
[5,] 2 2 2

You may also be interested in the rbind.fill.matrix() function from the "plyr" package, which will also let you bind matrices with differing columns, filling in with NA where necessary.

> m1 <- matrix(1, nrow=2, ncol=3)
> m2 <- matrix(2, nrow=3, ncol=4)
> l <- list(m1, m2)
> library(plyr)
> rbind.fill.matrix(l)
1 2 3 4
[1,] 1 1 1 NA
[2,] 1 1 1 NA
[3,] 2 2 2 2
[4,] 2 2 2 2
[5,] 2 2 2 2

How to combine matrices in lists of a list in R?

Use mapply with rbind like this:

do.call(mapply, c("rbind", mylist))

giving:

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

$B
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[3,] 1 3 1
[4,] 2 2 1
[5,] 3 2 1

Bind vectors across lists to single list of matrices

One option is to transpose the list of lists, then reduce the list elements to a single dataset with cbind.fill, get the transpose (t) and assign the row names to NULL

library(tidyverse)
library(rowr)
list(list1, list2, list3) %>%
transpose %>%
map(~ reduce(.x, cbind.fill, fill = NA) %>%
t %>%
`row.names<-`(NULL))
#$a
# [,1] [,2] [,3] [,4] [,5]
#[1,] 1 2 3 4 5
#[2,] 1 2 3 4 NA
#[3,] 1 2 3 NA NA

#$b
# [,1] [,2] [,3] [,4] [,5]
#[1,] 6 7 8 9 10
#[2,] 6 7 8 9 NA
#[3,] 6 7 8 NA NA

#$c
# [,1] [,2] [,3] [,4] [,5]
#[1,] 11 12 13 14 15
#[2,] 11 12 13 14 NA
#[3,] 11 12 13 NA NA

Or using base R

do.call(Map, c(f = function(...) {l1 <- list(...)
do.call(rbind, lapply(l1, `length<-`, max(lengths(l1))))},
mget(paste0("list", 1:3))))

Combine matrices with different arrays using loop FOR in R

Something like this?

src <- c(1, 2, 3, 4)
len <- length(src)
acc <- list()

for (i in 1:len) {
c <- t(combn(src, i))
zeros <- matrix(0, nrow = dim(c)[1], ncol = len - i)
acc[[i]] <- cbind(c, zeros)
}

do.call(rbind, acc)

Output:

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

Explanation:

  1. combn produces a matrix of combinations (albeit sideways from what you want, so transpose with t)
  2. make a matrix of zeros with number of rows = number of combinations and number of columns = total length - length of combination on this iteration
  3. bind column-wise
  4. outside the loop, bind all the accumulated matrices for each iteration row-wise.

BTW, I looked the the combinations function you were using and I'm not sure if you need that over the built-in combn function which is why I used it. combn just gives you all combinations of a certain length, which is what you want.

Combine list of lists of matrices into a list of matrices

purrr::pmap iterates in parallel over list items and passes to the function you specify, so you can obtain your desired results with just

library(purrr)

temp %>% pmap(cbind)
## [[1]]
## [,1] [,2] [,3] [,4]
## [1,] 1 3 3 3
## [2,] 1 3 3 3
##
## [[2]]
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 2 2 4 4 4 4
## [2,] 2 2 4 4 4 4

Combine matrices row by row

In the future, please include example data that is copy/pasteable, not just a picture.

m1 = matrix(1:6, ncol = 2)
m2 = matrix(7:12, ncol = 3)

combos = expand.grid(1:nrow(m1), 1:nrow(m2))
cbind(m1[combos$Var1, ], m2[combos$Var2, ])
# [,1] [,2] [,3] [,4] [,5]
# [1,] 1 4 7 9 11
# [2,] 2 5 7 9 11
# [3,] 3 6 7 9 11
# [4,] 1 4 8 10 12
# [5,] 2 5 8 10 12
# [6,] 3 6 8 10 12


Related Topics



Leave a reply



Submit