Reputation: 60858
For dealing with two-dimensional matrices, rbind
and cbind
are useful functions. Are there more generic functions to perform the same operation in more dimensions? Suppose I have data like this:
data <- lapply(c(11,22,33), function(i) matrix(i, nrow=2, ncol=4))
What I'd like to obtain is this:
data <- do.call(c, data)
dim(data) <- c(2, 4, 3)
but without having to work out all the dimensions myself.
Is there a function providing this functionality, either built-in or as part of a reasonably common package? Or do you want to share your own ideas of how such a function could be implemented most elegantly?
Bonus points:
aperm
could be avoided.list
of arguments, although using do.call
or list
, either one will suffice..combine
argument to a foreach call. So it should be able to construct multi-dimensional matrices using calls of the form f(f(f(a, b), c), d)
(each call takes exactly two arguments, the first usually the result of the previous call) or even f(f(a, b), c, d)
(more than two arguments, the first still might be the result of the previous call), with a, b, c, d
all of the same size, resulting in a matrix with a dimension 1 higher than the dimensions of these and a size of 4 in that dimension, corresponding to the 4 elements a
through d
.Upvotes: 1
Views: 99