[R] Is simplify2array working for dimension > 2?

Jean-Claude Arbaut @rb@utjc @end|ng |rom gm@||@com
Thu Feb 8 02:18:11 CET 2024

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,

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]]

More information about the R-help mailing list