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

标签 r arrays

我有一个 n 维数组,想要将一个 n-1 维数组绑定(bind)到跨特定维度集映射的开始/结束位置。

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

我认为这可以通过abind::abind()实现,但我不知道如何做到这一点。我尝试了abind::abind(array3D,array2D,along = c(3,2)),但是abind::abind()给了我一个错误,说它不允许 along 为多个维度。我还尝试了使用 abind::abind(array3D, array2D, around = 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")))

最佳答案

绑定(bind)维度超过 2 的数组可能会让人头脑扭曲,除了一点点试错之外,我并不总是知道如何解释它,直到关于维度的错误消失(并且预期的输出是已实现)。

在这种情况下,您的第二次尝试已接近完成,提示位于错误消息中:

arg 'X2' has dims=1, 4, 2; but need dims=X, 2, 4

对我来说,从 4, 22, 4(使用二维数组)听起来像是一个 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

我不知道如何在绑定(bind)调用中保留结果数组上的名称(粗略检查时没有参数组合做到这一点)。通过一些手动工作将其自动化应该是可行的,但我目前想不出一种容易自动化的方法。

关于r - 取n维数组并在r中绑定(bind)n-1维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58403490/

相关文章:

python - 在 R 中将 <dattm.dt> 转换为日期的有效方法

r - R中是否有像bigmemory这样的包可以处理大型列表对象?

java - 如何从文本文件创建对象数组?

javascript - 如何将对象添加到数组中但仅当数组的当前元素为 0 时?

arrays - 在 Ruby 中,我如何计算一个元素与其邻居的距离?

linux - 安装包时出现问题

r - 将一组分类变量转换为单个向量的函数

r - 基于 R 中另一个数据框中的多列删除数据框中的行

python - 使用python按字节修改图像

php - 如何列出修改过的文件?