R访问Array内部列表中的向量

问题描述 投票:1回答:1

我在R中有一个非常长的数组(1955x2417x1),其中每个位置存储一个长度为5的两个向量(名为“max”和“min”)的列表。

我想找到一种简单的方法来创建一个多维数组(dim 1955x2417x5),其中每个位置都包含来自向量“max”的单个值我已经查看了array of lists in r等答案

但到目前为止没有成功。我知道我可以使用数组的每个位置访问列表

myarray[posX, PosY][[1]][["max"]]

但是如何将它应用到整个数组呢?到目前为止我已经尝试过

newArray <- array( unlist(myarray[][[1]][["max"]]), c(1955, 2417, 5))

NewArray <-parApply(cl, myarray, c(1:2), function(x) {
  a=x[[1]][["max"]]
 } )

但结果不对。

你有什么建议吗?

arrays r list vector
1个回答
0
投票

e <- list(min = 1:3, max = 4:6)
arr <- array(list(e)[rep(1, 8)], c(2, 4))
dim(arr)
# [1] 2 4

然后有一个选择

res <- apply(arr, 1:2, function(x) x[[1]][["max"]])
dim(res)
# [1] 3 2 4

并且,如果维度的顺序很重要,

dim(aperm(res, c(2, 3, 1)))
# [1] 3 2 4
© www.soinside.com 2019 - 2024. All rights reserved.