取n维数组并在r中绑定n-1维数组

时间:2019-10-15 22:42:15

标签: r arrays

我有一个n维数组,并且想将n-1维数组绑定到在一组特定维上映射的起点/终点。

这是问题的简化版本:我有一个3D数组,并且想将2D数组(也称为矩阵)绑定到3D数组的末尾。具体来说,我希望2D数组的行映射到3D数组的deps,并且我希望2D数组的cols映射到3D数组的cols(也就是将矩阵切片添加到数组的底部) 。从视觉上看,如果我们想到一堆R编程书籍(3D数组),这就像在堆栈底部添加了Hadley Wickham的最新R书籍(2D数组)。

我认为abind::abind()可以做到这一点,但是我不知道该怎么做。我尝试了abind::abind(array3D, array2D, along = c(3,2)),但是abind::abind()给了我一个错误,说它不允许along是多维的。我也用abind::abind(array3D, array2D, along = 1)尝试了“相反”操作,但仍然出现错误,提示尺寸不正确。

打个比方,我的目标在概念上类似于rbind(matrix, vector)

以下是可重现的示例:

library(abind)
array3D <- array(data = c(111, 211, 311,
                          121, 221, 321,
                             112, 212, 312,
                             122, 222, 322,
                                113, 213, 313,
                                123, 223, 323,
                                   114, 214, 314,
                                   124, 224, 324),
                 dim = c(3,2,4),
                 dimnames = list("row" = c("row1","row2","row3"),
                                 "col" = c("col1","col2"),
                                 "dep" = c("dep1","dep2","dep3","dep4")))

array2D <- array(data = c(411, 412, 413, 414,
                          421, 422, 423, 424),
                 dim = c(4,2),
                 dimnames = list("dep" = c("dep1","dep2","dep3","dep4"),
                                 "col" = c("col1","col2")))

abindError <- abind(array3D, array2D, along = c(3,2))

Error in abind(array3D, array2D, along = c(3, 2)) : "along" must specify one dimension of the array, or interpolate between two dimensions of the array

abindError <- abind(array3D, array2D, along = 1)

Error in abind(array3D, array2D, along = 1) : arg 'X2' has dims=1, 4, 2; but need dims=X, 2, 4

desiredResult <- array(data = c(111, 211, 311, 411,
                                121, 221, 321, 421,
                                   112, 212, 312, 412,
                                   122, 222, 322, 422,
                                      113, 213, 313, 413,
                                      123, 223, 323, 423, 
                                         114, 214, 314, 414,
                                         124, 224, 324, 424),
               dim = c(4,2,4),
               dimnames = list("row" = c("row1","row2","row3","row4"),
                               "col" = c("col1","col2"),
                               "dep" = c("dep1","dep2","dep3","dep4")))

1 个答案:

答案 0 :(得分:2)

绑定暗度超过2的数组可能会令人不寒而栗,我一直不知道如何解释它,只是经过反复尝试,直到有关尺寸的错误消失(并且预期的输出是实现)。

在这种情况下,您的第二次尝试已结束,并且提示出现在错误消息中:

  

arg'X2'具有dims = 1,4,2;但需要dims = X,2,4

4, 22, 4(带有2D数组)对我来说听起来像t变换,所以我们可以使用它:

abind::abind(array3D, t(array2D), along = 1)
# , , dep1
#      col1 col2
# row1  111  121
# row2  211  221
# row3  311  321
#       411  421
# , , dep2
#      col1 col2
# row1  112  122
# row2  212  222
# row3  312  322
#       412  422
# , , dep3
#      col1 col2
# row1  113  123
# row2  213  223
# row3  313  323
#       413  423
# , , dep4
#      col1 col2
# row1  114  124
# row2  214  224
# row3  314  324
#       414  424

all(abind::abind(array3D, t(array2D), along = 1) == desiredResult)
# [1] TRUE
### except for the names, of course

我不知道如何在绑定调用中保留结果数组上的名称(在粗略检查中没有参数的组合)。通过一些手动操作将其自动化应该是可行的,但是我现在还无法想到一种易于自动化的方法。