如何放置尺寸可变的物体插入R中的数组? [英] How to place objects of variable dimensions into an array in R?

查看:102
本文介绍了如何放置尺寸可变的物体插入R中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,它是三维。在阵列的每一个单元,我需要与列表,填补它在(或另一个阵列或矩阵,只需要一系列值)。然而,每个清单/阵列的要被存储在所述细胞是不同的尺寸。

I have an array that is 3 dimensional. In each cell of the array, I need to fill it in with a list, (or another array, or matrix, just needs to be a series of values). However, each of the lists/arrays to be stored in said cells are of different sizes.

下面是我如何创建数组:

Here's how I create the array:

AR< - 阵列(0,C(365,6,4))

AR [3,2,1] =列表(3,2,4,5,3,2)#抛出错误

> ar[3,2,1]=array(3,c(1,2))
Error in ar[3, 2, 1] = array(3, c(1, 2)) : 
  number of items to replace is not a multiple of replacement length

这也不起作用如果我尝试初始化与空数组,空列表或NA,而不是零值的数组。

It also doesn't work If I try to initialize the array with empty arrays, empty lists or NA instead of zero values.

解决这个问题没有简单的方法?三维阵列中这些对象必须是容易可转位。谢谢。

Any easy way around this problem? these objects in the 3-D array must be easily indexable. Thanks.

推荐答案

这似乎笨拙给我,但我认为这是你问的:

This seems unwieldy to me, but I think it's what you're asking for:

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更长的单元格内容中的额外列表)分配时他们。

Note the need to wrap cell contents longer than length=1 in an extra list() when assigning them.

这篇关于如何放置尺寸可变的物体插入R中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆