Given this list of matrices:
my_list <- list(
D = matrix(
data = 1:12,
nrow = 3,
ncol = 4,
dimnames = list(c("A", "B", "C"),
c("F", "G", "H", "I"))),
E = matrix(
data = 13:24,
nrow = 3,
ncol = 4,
dimnames = list(c("A", "B", "C"),
c("F", "G", "H", "I")))
)
Yielding:
$D
F G H I
A 1 4 7 10
B 2 5 8 11
C 3 6 9 12
$E
F G H I
A 13 16 19 22
B 14 17 20 23
C 15 18 21 24
I want to convert it to a 3D array table object, like so:
my_tbl <- as.table(
x = array(
data = unlist(my_list),
dim = c(3, 4, 2),
dimnames = list(
X = c("A", "B", "C"),
Y = c("F", "G", "H", "I"),
Z = c("D", "E"))))
Yielding:
, , Z = D
Y
X F G H I
A 1 4 7 10
B 2 5 8 11
C 3 6 9 12
, , Z = E
Y
X F G H I
A 13 16 19 22
B 14 17 20 23
C 15 18 21 24
However, due to the call to unlist()
, this turns out to either be rather slow with big matrices or result in a vector memory exhausted (limit reached?)
-error, simply because the big matrices are coerced to one really big vector.
So, any ideas on how to do this conversion efficiently?