R中的累积多维数组和

时间:2019-05-01 12:06:12

标签: arrays r apply

在R中,如何将矩阵不同边距上的累加总和的计算概括为多维数组?

例如,给定矩阵

a2 <- array(1:6, dim = c(2,3))
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

可以使用apply计算不同边距的累积总和:

apply(a2, 2, cumsum)
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    3    7   11
t(apply(a2, 1, cumsum))
     [,1] [,2] [,3]
[1,]    1    4    9
[2,]    2    6   12

请注意,在后一种情况下,需要进行一些重塑。现在的问题是:

您将如何计算多维数组的累积总和?

例如,对于像这样的三维数组:

a3 <- array(1:24, dim = c(2,3,4))

我对行,列和第三维的累加总和感兴趣,保留了原始数组的结构。具体来说,该行的累计总和应为:

, , 1

     [,1] [,2] [,3]
[1,]    1    4    9
[2,]    2    6   12

, , 2

     [,1] [,2] [,3]
[1,]    7   16   27
[2,]    8   18   30

, , 3

     [,1] [,2] [,3]
[1,]   13   28   45
[2,]   14   30   48

, , 4

     [,1] [,2] [,3]
[1,]   19   40   63
[2,]   20   42   66

n维数组的答案是什么?

4 个答案:

答案 0 :(得分:3)

一种方法是使用旧的for循环

res <- a3
for (k in 1:dim(a3)[3]) res[, , k] <- t(apply(a3[, , k], 1, cumsum))
res
#, , 1
#
#     [,1] [,2] [,3]
#[1,]    1    4    9
#[2,]    2    6   12
#
#, , 2
#
#     [,1] [,2] [,3]
#[1,]    7   16   27
#[2,]    8   18   30
#
#, , 3
#
#     [,1] [,2] [,3]
#[1,]   13   28   45
#[2,]   14   30   48
#
#, , 4
#
#     [,1] [,2] [,3]
#[1,]   19   40   63
#[2,]   20   42   66

答案 1 :(得分:1)

这几乎可以满足您的要求,但是结果是转置的

apply(a3, c(1, 3), cumsum)

#, , 1

#     [,1] [,2]
#[1,]    1    2
#[2,]    4    6
#[3,]    9   12

#, , 2

#     [,1] [,2]
#[1,]    7    8
#[2,]   16   18
#[3,]   27   30

#, , 3

#     [,1] [,2]
#[1,]   13   14
#[2,]   28   30
#[3,]   45   48

#, , 4

#     [,1] [,2]
#[1,]   19   20
#[2,]   40   42
#[3,]   63   66

我不知道如何在相同的apply调用中转置结果(应该有一种方法)。我尝试过

t(apply(a3, c(1, 3), cumsum))
apply(a3, c(1, 3), function(x) t(cumsum(x)))

但这不起作用。但是,现在,如果我们再次使用apply并转置,我们可以恢复原始结构。

apply(apply(a3, c(1, 3), cumsum), c(1, 3), t)

答案 2 :(得分:1)

使用apply,然后使用aperm。唯一棘手的部分是正确设置边距:

aperm(apply(a3, -2, cumsum), c(2, 1, 3))

其中的每一个也可以工作:

aperm(apply(a3, c(1, 3), cumsum), c(2, 1, 3))

aperm(apply(a3, c(3, 1), cumsum), c(3, 1, 2))

apply(apply(a3, -2, cumsum), -2, c)

apply(apply(a3, c(1, 3), cumsum), c(1, 3), c)

library(plyr)
aa <- aperm(aaply(a3, c(1, 3), cumsum), c(1, 3, 2))
dimnames(aa) <- NULL

答案 3 :(得分:0)

从@G推断。格洛腾迪克的答案,此函数使用aperm来计算n维数组任何边距上的累积和:

array_cumsum <- function(a, margin) {
  n <- length(dim(a))
  permorder <- append(x = 2:n, 1, margin - 1)
  aperm(apply(a, -margin, cumsum), permorder)
}

例如,使用一个简单的由1组成的数组,以便轻松查看累积和,该函数可用于计算第二维上的边距:

a <- array(1, dim = c(2,3,4))
array_cumsum(a3, 2)
# , , 1
# 
# [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    1    2    3
# 
# , , 2
# 
# [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    1    2    3
# 
# , , 3
# 
# [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    1    2    3
# 
# , , 4
# 
# [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    1    2    3

以及第3维:

array_cumsum(a3, 3)
# , , 1
# 
# [,1] [,2] [,3]
# [1,]    1    1    1
# [2,]    1    1    1
# 
# , , 2
# 
# [,1] [,2] [,3]
# [1,]    2    2    2
# [2,]    2    2    2
# 
# , , 3
# 
# [,1] [,2] [,3]
# [1,]    3    3    3
# [2,]    3    3    3
# 
# , , 4
# 
# [,1] [,2] [,3]
# [1,]    4    4    4
# [2,]    4    4    4