Search code examples
rlistlapplycbind

cbind each matrix of list with each row of each matrix in another list


How to make

The first has 3 elements. Each element is 5*4 matrix. Let's call these elements m1, m2, m3

The second has 3 elements too. Each element is 5*4 matrix. Let's call these elements n1, n2, n3

set.seed(1001)
first<-replicate(3,list(matrix(unlist(replicate(5,sample(c(1,2,3,4),4,replace=TRUE))),ncol=4)))
second<-replicate(3,list(matrix(unlist(replicate(5,sample(c(5,6,7,8),4,replace=TRUE))),ncol=4)))

> first
[[1]]
     [,1] [,2] [,3] [,4]
[1,]    4    4    2    4
[2,]    2    1    1    1
[3,]    2    1    4    2
[4,]    2    2    2    1
[5,]    2    4    2    4

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

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

> second
[[1]]
     [,1] [,2] [,3] [,4]
[1,]    7    7    8    5
[2,]    7    8    8    5
[3,]    7    5    8    7
[4,]    6    5    8    7
[5,]    6    6    6    5

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

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

What I want is cbind each element in first with each column of each element in second.

n1 has 4 columns. Let's call each column n1c1, n1c2, n1c3, n1c4.

So, I want to generate m1+n1c1, m1+n1c2, m1+n1c3, m1+n1c4.

Other things go on like this.

m2+n2c1, m2+n2c2, m2+n2c3, m2+n2c4

m3+n3c1, m3+n3c2, m3+n3c3, m3+n3c4

These will be like this.

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

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

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

...

[[12]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    1    2    3    6
[2,]    1    4    2    2    8
[3,]    3    1    1    1    5
[4,]    1    3    3    1    6
[5,]    4    3    3    4    5

I tried these code. But it all failed.

out<-lapply(first, function(x) (cbind(first, second=x)))
out

[[1]]
     first             
[1,] Numeric,20 1 1 4 2
[2,] Numeric,20 2 2 1 4
[3,] Numeric,20 4 1 4 3
[4,] Numeric,20 4 4 2 1
[5,] Numeric,20 2 3 4 4

[[2]]
     first             
[1,] Numeric,20 1 3 1 4
[2,] Numeric,20 3 1 3 2
[3,] Numeric,20 2 2 4 1
[4,] Numeric,20 4 4 3 3
[5,] Numeric,20 2 4 1 3

[[3]]
     first             
[1,] Numeric,20 3 1 4 1
[2,] Numeric,20 4 2 2 1
[3,] Numeric,20 3 2 1 1
[4,] Numeric,20 2 2 3 1
[5,] Numeric,20 2 1 4 4

(I didn't even know what is 'Numeric,20'!)

And this even failed.

out<-lapply(first, function(x) lapply(1:nrow(x), function(y) cbind(first, second=y)))
out

[[1]]
[[1]][[1]]
     first      second
[1,] Numeric,20 1     
[2,] Numeric,20 1     
[3,] Numeric,20 1     

[[1]][[2]]
     first      second
[1,] Numeric,20 2     
[2,] Numeric,20 2     
[3,] Numeric,20 2     

[[1]][[3]]
     first      second
[1,] Numeric,20 3     
[2,] Numeric,20 3     
[3,] Numeric,20 3     

[[1]][[4]]
     first      second
[1,] Numeric,20 4     
[2,] Numeric,20 4     
[3,] Numeric,20 4     

[[1]][[5]]
     first      second
[1,] Numeric,20 5     
[2,] Numeric,20 5     
[3,] Numeric,20 5     


[[2]]
[[2]][[1]]
     first      second
[1,] Numeric,20 1     
[2,] Numeric,20 1     
[3,] Numeric,20 1     

[[2]][[2]]
     first      second
[1,] Numeric,20 2     
[2,] Numeric,20 2     
[3,] Numeric,20 2     

[[2]][[3]]
     first      second
[1,] Numeric,20 3     
[2,] Numeric,20 3     
[3,] Numeric,20 3     

[[2]][[4]]
     first      second
[1,] Numeric,20 4     
[2,] Numeric,20 4     
[3,] Numeric,20 4     

[[2]][[5]]
     first      second
[1,] Numeric,20 5     
[2,] Numeric,20 5     
[3,] Numeric,20 5     


[[3]]
[[3]][[1]]
     first      second
[1,] Numeric,20 1     
[2,] Numeric,20 1     
[3,] Numeric,20 1     

[[3]][[2]]
     first      second
[1,] Numeric,20 2     
[2,] Numeric,20 2     
[3,] Numeric,20 2     

[[3]][[3]]
     first      second
[1,] Numeric,20 3     
[2,] Numeric,20 3     
[3,] Numeric,20 3     

[[3]][[4]]
     first      second
[1,] Numeric,20 4     
[2,] Numeric,20 4     
[3,] Numeric,20 4     

[[3]][[5]]
     first      second
[1,] Numeric,20 5     
[2,] Numeric,20 5     
[3,] Numeric,20 5  

(This look similar, but it failed too.)

How can i solve this problem?


Solution

  • You could use expand.grid

    Map(function(x) cbind(first[[x[1]]], second[[x[1]]][, x[2]]),  
        data.frame(t(rev(expand.grid(seq(ncol(first[[1]])), seq(length(second)))))))
    

    Yields

    # $X1
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    3    3    4    1    5
    # [2,]    3    4    4    1    8
    # [3,]    3    3    4    3    5
    # [4,]    3    2    4    2    8
    # [5,]    4    2    4    3    6
    # 
    # $X2
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    3    3    4    1    8
    # [2,]    3    4    4    1    7
    # [3,]    3    3    4    3    6
    # [4,]    3    2    4    2    7
    # [5,]    4    2    4    3    6
    # 
    # $X3
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    3    3    4    1    8
    # [2,]    3    4    4    1    8
    # [3,]    3    3    4    3    6
    # [4,]    3    2    4    2    6
    # [5,]    4    2    4    3    8
    # 
    # $X4
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    3    3    4    1    8
    # [2,]    3    4    4    1    5
    # [3,]    3    3    4    3    5
    # [4,]    3    2    4    2    5
    # [5,]    4    2    4    3    7
    # 
    # $X5
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    2    1    2    4    5
    # [2,]    2    4    3    1    7
    # [3,]    4    2    4    4    7
    # [4,]    3    3    1    2    6
    # [5,]    4    2    2    4    7
    # 
    # $X6
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    2    1    2    4    7
    # [2,]    2    4    3    1    7
    # [3,]    4    2    4    4    6
    # [4,]    3    3    1    2    6
    # [5,]    4    2    2    4    7
    # 
    # $X7
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    2    1    2    4    5
    # [2,]    2    4    3    1    5
    # [3,]    4    2    4    4    8
    # [4,]    3    3    1    2    6
    # [5,]    4    2    2    4    6
    # 
    # $X8
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    2    1    2    4    6
    # [2,]    2    4    3    1    5
    # [3,]    4    2    4    4    8
    # [4,]    3    3    1    2    5
    # [5,]    4    2    2    4    6
    # 
    # $X9
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    3    1    1    1    7
    # [2,]    2    2    1    2    6
    # [3,]    2    4    3    4    7
    # [4,]    2    3    1    4    5
    # [5,]    4    1    4    4    6
    # 
    # $X10
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    3    1    1    1    7
    # [2,]    2    2    1    2    8
    # [3,]    2    4    3    4    5
    # [4,]    2    3    1    4    6
    # [5,]    4    1    4    4    6
    # 
    # $X11
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    3    1    1    1    5
    # [2,]    2    2    1    2    6
    # [3,]    2    4    3    4    8
    # [4,]    2    3    1    4    6
    # [5,]    4    1    4    4    7
    # 
    # $X12
    #      [,1] [,2] [,3] [,4] [,5]
    # [1,]    3    1    1    1    8
    # [2,]    2    2    1    2    8
    # [3,]    2    4    3    4    5
    # [4,]    2    3    1    4    7
    # [5,]    4    1    4    4    7
    

    Data

    first <- list(structure(c(3, 3, 3, 3, 4, 3, 4, 3, 2, 2, 4, 4, 4, 4, 4, 
    1, 1, 3, 2, 3), .Dim = 5:4), structure(c(2, 2, 4, 3, 4, 1, 4, 
    2, 3, 2, 2, 3, 4, 1, 2, 4, 1, 4, 2, 4), .Dim = 5:4), structure(c(3, 
    2, 2, 2, 4, 1, 2, 4, 3, 1, 1, 1, 3, 1, 4, 1, 2, 4, 4, 4), .Dim = 5:4))
    
    second <- list(structure(c(5, 8, 5, 8, 6, 8, 7, 6, 7, 6, 8, 8, 6, 6, 8, 
    8, 5, 5, 5, 7), .Dim = 5:4), structure(c(5, 7, 7, 6, 7, 7, 7, 
    6, 6, 7, 5, 5, 8, 6, 6, 6, 5, 8, 5, 6), .Dim = 5:4), structure(c(7, 
    6, 7, 5, 6, 7, 8, 5, 6, 6, 5, 6, 8, 6, 7, 8, 8, 5, 7, 7), .Dim = 5:4))