将列表的(m,m,n)阵列组合成(m,m,n)阵列

时间:2014-11-17 23:43:12

标签: arrays r

将m,m,n维存在一系列列表中的m,m,n维数组合成一个m,m,n维数组的快速而肮脏的方法是什么?

示例:

以下是三个包含m,m,n维数组的列表:

list1 <- array (1, dim = c(5, 5, 3)) 
list2 <- array (2, dim = c(5, 5, 3)) 
list3 <- array (3, dim = c(5, 5, 3))

m,m,n维度的组合列表:

lists <- list(list1 = list1, list2 = list2, list3 = list3)

我想在&#34;列表&#34;上执行一项功能。给我一个m,m,n维数组的对象( 例如&#34; want.to.get&#34;输出

want.to.get <- array (rep (1:3, each = 5*5*3), dim = c(5,5,9))

2 个答案:

答案 0 :(得分:5)

library(abind)
abind(lists, new.names = list(NULL, NULL, 1:9))

答案 1 :(得分:1)

或者你可以unlist并在dim

中指定array
dim1 <- dim(lists[[1]])*c(1,1,length(lists))
ar1 <- array(unlist(lists, use.names=FALSE), dim=dim1)
all.equal(ar1, want.to.get)
#[1] TRUE