【问题标题】:R Accessing vector inside list inside ArrayR访问数组内列表内的向量
【发布时间】:2018-07-26 12:42:59
【问题描述】:

我在 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】:

    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
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      相关资源
      最近更新 更多