[R] list of matrices --> array

Rolf Turner rolf.turner at xtra.co.nz
Thu Feb 14 09:53:20 CET 2013


require(abind)
do.call(abind,c(my_list,list(along=0))) # Gives 2 x 4 x 5
do.call(abind,c(my_list,list(along=3))) # Gives 4 x 5 x 2

The latter seems more natural to me.

     cheers,

         Rolf Turner

On 02/14/2013 07:03 PM, Murat Tasan wrote:
> i'm somehow embarrassed to even ask this, but is there any built-in
> method for doing this:
>
> my_list <- list()
> my_list[[1]] <- matrix(1:20, ncol = 5)
> my_list[[2]] <- matrix(20:1, ncol = 5)
>
> now, knowing that these matrices are identical in dimension, i'd like
> to unfold the list to a 2x4x5 (or some other permutation of the dim
> sizes) array.
> i know i can initialize the array, then loop through my_list to fill
> the array, but somehow this seems inelegant.
> i also know i can vectorize the matrices and unlist the list, then
> build the array from that single vector, but this also seems inelegant
> (and an easy place to introduce errors/bugs).
>
> i can't seem to find any built-in that handles this already... but
> maybe i just haven't looked hard enough :-/



More information about the R-help mailing list