R Accessing vector inside list inside Array

Refresh

April 2019

Views

216 time

1

I have a very long Array (1955x2417x1) in R where each position stores a list of two vector (named "max" and "min") of length 5.

I would like to find a simple way to create a multidimensional array (dim 1955x2417x5) where each position holds a single value from vector "max" I have looked at answers such as array of lists in r

but so far without success. I know I can access the list in each position of the array using

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

but how to apply that to the whole Array? SO far I have tried

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

and

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

but the results are not right.

Do you have any suggestion?

1 answers

0

Let

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

Then one option is

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

and, if the order of dimensions matters,

dim(aperm(res, c(2, 3, 1)))
# [1] 3 2 4