Reading the doc for ?simplify2array, I got the impression that with the
'higher = T' argument the function returns an array of dimension greater
than 2 when it makes sense (the doc says "when appropriate", which is
rather vague). I would expect

a <- list(
  list(list(1, 2), list(3, 4)),
  list(list(5, 6), list(7, 8))
)
simplify2array(a, higher = T)

to return the same (possibly up to a dimension permutation) as
array(1:8, dim = c(2, 2, 2))

However, in this case simplify2array returns a matrix (i.e. 2 dimensional
array), whose elements are lists.
It's the same as
structure(list(list(1, 2), list(3, 4), list(5, 6), list(7, 8)), dim = c(2,
2))

I get the same behavior with
a <- list(
  list(c(1, 2), c(3, 4)),
  list(c(5, 6), c(7, 8))
)
but then the matrix elements are numeric vectors instead of lists.

Did I miss something to get the result I expected with this function? Or is
it a bug? Or maybe the function is not supposed to return a higher
dimensional array, and I didn't understand the documentation correctly?

There is a workaround, one can do for instance
array(unlist(a), dim = c(2, 2, 2))
and there may be better options (checking dimensions?).

In case it's important: running R 4.3.2 on Debian 12.4.

Best regards,

Jean-Claude Arbaut

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to