[R] list of matrices --> array

arun smartpink111 at yahoo.com
Thu Feb 14 07:45:44 CET 2013


Hi,

May be this helps:
library(plyr)
res<-aaply(laply(my_list, as.matrix),c(2,3),function(x) x)
attr(res,"dimnames")<- NULL
 identical(res,foo)
#[1] TRUE
A.K.




----- Original Message -----
From: Murat Tasan <mmuurr at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Thursday, February 14, 2013 1:13 AM
Subject: Re: [R] list of matrices --> array

FYI - this is my current method, but somehow i just don't like it ;-)

foo <- array(NA, dim = c(4,5,length(my_list)))
for(k in 1:length(my_list)) {
  foo[,,k] <- my_list[[k]]
}

-m

On Thu, Feb 14, 2013 at 1:03 AM, Murat Tasan <mmuurr at gmail.com> 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 :-/
>
> cheers,
>
> -m

______________________________________________
R-help at r-project.org mailing list
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.




More information about the R-help mailing list