将列表的每个矩阵与另一个列表中的每个矩阵的每一行绑定

时间:2019-07-09 09:12:57

标签: r list lapply cbind

制作方法

第一个包含3个元素。每个元素是5 * 4矩阵。我们将这些元素称为m1m2m3

第二个元素也包含3个元素。每个元素是5 * 4矩阵。我们将这些元素称为n1n2n3

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

我想要的是将first中的每个元素与second中每个元素的每一列绑定。

n1有4列。让我们将每个列称为n1c1n1c2n1c3n1c4

因此,我想生成m1 + n1c1m1 + n1c2m1 + n1c3m1 + n1c4

其他事情会像这样

m2 + n2c1m2 + n2c2m2 + n2c3m2 + n2c4

m3 + n3c1m3 + n3c2m3 + n3c3m3 + n3c4

这些将是这样。

[[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

我尝试了这些代码。但这一切都失败了。

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

(我什至都​​不知道'Numeric,20'!)

这甚至失败了。

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  

(这看起来很相似,但是也失败了。)

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您可以尝试使用嵌套的lapply

lapply(seq_along(first), function(x) lapply(seq_len(ncol(second[[x]])), 
                    function(y) cbind(first[[x]], second[[x]][, y])))


#[[1]]
#[[1]][[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

#[[1]][[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
#....

或者使用Map

Map(function(x, y) lapply(seq_len(ncol(y)), function(i) cbind(x, y[,i])),
                   first, second)

答案 1 :(得分:1)

您可以使用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)))))))

产量

# $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

数据

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))