Arrays 如何将可变维度的对象放入R中的数组中?

Arrays 如何将可变维度的对象放入R中的数组中?,arrays,r,multidimensional-array,Arrays,R,Multidimensional Array,我有一个三维数组。在数组的每个单元格中,我需要用一个列表填充它,或者另一个数组,或者矩阵,只需要是一系列值。然而,要存储在所述单元中的每个列表/阵列具有不同的大小 以下是我创建阵列的方法: ar这对我来说似乎很难,但我认为这正是你想要的: ar <- array(list(), dim=c(5, 3, 2)) ar[1, 1, 1] <- list(list(1, 2, 3, 4, 5)) ar[2, 1, 1] <- list(matrix(seq(4), nrow=2))

我有一个三维数组。在数组的每个单元格中,我需要用一个列表填充它,或者另一个数组,或者矩阵,只需要是一系列值。然而,要存储在所述单元中的每个列表/阵列具有不同的大小

以下是我创建阵列的方法:


ar这对我来说似乎很难,但我认为这正是你想要的:

ar <- array(list(), dim=c(5, 3, 2))
ar[1, 1, 1] <- list(list(1, 2, 3, 4, 5))
ar[2, 1, 1] <- list(matrix(seq(4), nrow=2))
ar[3, 1, 1] <- "foo"
ar[4, 1, 1] <- list(array(-1, dim=c(5, 7, 9)))
ar
# , , 1
# 
#      [,1]        [,2] [,3]
# [1,] List,5      NULL NULL
# [2,] Integer,4   NULL NULL
# [3,] "foo"       NULL NULL
# [4,] Numeric,315 NULL NULL
# [5,] NULL        NULL NULL
# 
# , , 2
# 
#      [,1] [,2] [,3]
# [1,] NULL NULL NULL
# [2,] NULL NULL NULL
# [3,] NULL NULL NULL
# [4,] NULL NULL NULL
# [5,] NULL NULL NULL

请注意,分配时需要将长度大于1的单元格内容包装到额外列表中。

比较dimar和lengthlist3、2、4、5、3、2。结果分别为365、6和4,……和……6。我不太清楚,这是什么额外的信息?请记住,我试图用另一个数组填充多维数组365×4×5的每个单元格。每个单元格都将填充不同大小的数组。是否要用6个值填充8760个单元格?此外,ar[3,2,1]是一个值。您可能希望ar是一个列表,而不是数组。数组是一个n维矩阵。从本质上来说,你不能在其中插入列表。所以,似乎没有友好的解决方案@Pascal,就像我在问题中所说的:如果我尝试用空数组、空列表或NA而不是零值初始化数组,它也不起作用。
ar <- array(list(), dim=c(5, 3, 2))
ar[1, 1, 1] <- list(list(1, 2, 3, 4, 5))
ar[2, 1, 1] <- list(matrix(seq(4), nrow=2))
ar[3, 1, 1] <- "foo"
ar[4, 1, 1] <- list(array(-1, dim=c(5, 7, 9)))
ar
# , , 1
# 
#      [,1]        [,2] [,3]
# [1,] List,5      NULL NULL
# [2,] Integer,4   NULL NULL
# [3,] "foo"       NULL NULL
# [4,] Numeric,315 NULL NULL
# [5,] NULL        NULL NULL
# 
# , , 2
# 
#      [,1] [,2] [,3]
# [1,] NULL NULL NULL
# [2,] NULL NULL NULL
# [3,] NULL NULL NULL
# [4,] NULL NULL NULL
# [5,] NULL NULL NULL