如何在每个第n个矩阵之后将列表中的矩阵插入到不同的矩阵列表中?

时间:2017-11-10 12:52:53

标签: r list matrix

示例数据:

第一个矩阵列表:

c1 <- matrix(rnorm(10),5,2)
c2 <- c1+(rnorm(10))
c3 <- c1+(rnorm(10))
c4 <- c1+(rnorm(10))
c5 <- c1+(rnorm(10))
c6 <- c1+(rnorm(10))
clist <- list(c1,c2,c3,c4,c5,c6)

    [[1]]
            [,1]        [,2]
[1,]  1.16937929 -0.44686704
[2,] -1.71374929 -0.05024722
[3,] -0.53382340 -0.04600470
[4,] -0.04044502 -1.82671547
[5,] -1.70016440 -0.29055706

[[2]]
           [,1]       [,2]
[1,]  1.8261409  1.2420189
[2,] -3.8692178  1.1538126
[3,]  0.3467076 -0.7718303
[4,] -3.1253087 -1.5639540
[5,] -2.0631379 -0.6334828

[[3]]
           [,1]       [,2]
[1,] -0.3836731 -2.6561491
[2,] -3.4370839 -0.7189812
[3,] -0.8897828  0.8880357
[4,] -0.9525466 -2.5769396
[5,] -2.1867013  0.3046946

[[4]]
           [,1]       [,2]
[1,]  0.6109367 -1.0488075
[2,] -2.0091019 -0.8754184
[3,] -1.7069800 -0.2801976
[4,]  0.9334261 -3.8255870
[5,] -1.6822699  0.9734554

[[5]]
           [,1]       [,2]
[1,]  1.7572672 -0.2234586
[2,] -2.4024455  0.4508621
[3,] -0.7868861 -1.6521005
[4,]  0.2301057 -1.7658791
[5,]  0.2453109  0.3295073

[[6]]
           [,1]       [,2]
[1,]  0.5374283 -1.7285851
[2,] -2.5346283 -0.5007830
[3,] -1.6930066 -0.8153968
[4,]  0.9397925 -0.4508327
[5,] -3.2985109  0.7507062

第二个矩阵列表:

a1 <- matrix(rnorm(10),5,2)
a2 <- a1+(rnorm(10))
a3 <- a1+(rnorm(10))
alist <- list(a1, a2, a3)

[[1]]
           [,1]       [,2]
[1,] -0.3757278 -0.1486482
[2,] -0.3005715  1.3207322
[3,] -0.2823023 -0.6269376
[4,] -0.3914970  0.8810226
[5,]  1.3278155  1.2826003

[[2]]
           [,1]      [,2]
[1,]  0.2722553 0.4236911
[2,] -1.2992684 0.9879655
[3,] -0.8374446 0.4617695
[4,]  1.6460518 1.5344395
[5,]  1.0890759 3.7607345

[[3]]
           [,1]        [,2]
[1,] -0.7965729 -0.02635211
[2,] -0.3065876  0.05762188
[3,]  0.5919724 -1.48663558
[4,]  1.1887124  1.47474007
[5,]  0.8573545  1.35161592

我想在列表a1中的alistc2之间插入来自c3的{​​{1}},然后从{{{}插入clist 1 {}在a2alist之间,c4c5之后。

因此,模式是在a3中的每个第二个矩阵之后插入c6的每个连续矩阵,因此它看起来像alist。因为我的数据会大得多,所以我需要一些可以应用于大型数据集的东西。我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

您可以以合理的方式命名两个列表,将它们组合成一个列表,并按名称将它们组合在一起。

res

然后您有以下顺序:

names(clist) <- paste0("c", seq_along(clist))
names(alist) <- paste0("c", seq_along(alist)*2, "a")
x <- c(clist, alist)
x <- x[order(names(x))]