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
list of matrices --> array
7 messages · arun, Rolf Turner, David Winsemius +2 more
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
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.
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 :-/
On Feb 13, 2013, at 10: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 :-/
I think the built-in function you seek is `simplify2array`:
# gives 4 x 5 x 2
> simplify2array(my_list)
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 20 16 12 8 4
[2,] 19 15 11 7 3
[3,] 18 14 10 6 2
[4,] 17 13 9 5 1
> # 2 x 4 x 5
aperm( simplify2array(my_list), c(3,1,2) )
# 4 x 2 x 5
> aperm( simplify2array(my_list), c(1,3,2) )
David Winsemius, MD Alameda, CA, USA
3 days later
abind() (from package 'abind') can take a list of arrays as its first argument, so in general, no need for do.call() with abind().
As another poster pointed out, simplify2array() can also be used; while abind() gives more options regarding which dimension is created and how dimension names are constructed.
> x <- list(A=cbind(X=c(a=1,b=2,c=3,d=4),Y=5:8,Z=9:12), B=cbind(X=c(a=13,b=14,c=15,d=16),Y=17:20,Z=21:24))
$A
X Y Z
a 1 5 9
b 2 6 10
c 3 7 11
d 4 8 12
$B
X Y Z
a 13 17 21
b 14 18 22
c 15 19 23
d 16 20 24
>
> dim(abind(x, along=3))
[1] 4 3 2
> dim(abind(x, along=1.5))
[1] 4 2 3
> dim(abind(x, along=0.5))
[1] 2 4 3
> dim(abind(x, along=1, hier.names=T)) # construct rownames in a hierarchical manner A.a, A.b, etc
[1] 8 3
> dim(abind(x, along=2, hier.names=T)) # construct colnames in a hierarchical manner
[1] 4 6
> abind(x, along=2, hier.names=T)
A.X A.Y A.Z B.X B.Y B.Z
a 1 5 9 13 17 21
b 2 6 10 14 18 22
c 3 7 11 15 19 23
d 4 8 12 16 20 24
>
On 2/14/2013 3:53 AM, Rolf Turner wrote:
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 :-/
______________________________________________ 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.
thanks to all! didn't know about simplify2array, nor about the abind package. they're exactly what i wanted. cheers, -m
On Sun, Feb 17, 2013 at 9:41 AM, Tony Plate <taplate at gmail.com> wrote:
abind() (from package 'abind') can take a list of arrays as its first argument, so in general, no need for do.call() with abind(). As another poster pointed out, simplify2array() can also be used; while abind() gives more options regarding which dimension is created and how dimension names are constructed.
x <- list(A=cbind(X=c(a=1,b=2,c=3,d=4),Y=5:8,Z=9:12), B=cbind(X=c(a=13,b=14,c=15,d=16),Y=17:20,Z=21:24))
$A X Y Z a 1 5 9 b 2 6 10 c 3 7 11 d 4 8 12 $B X Y Z a 13 17 21 b 14 18 22 c 15 19 23 d 16 20 24
dim(abind(x, along=3))
[1] 4 3 2
dim(abind(x, along=1.5))
[1] 4 2 3
dim(abind(x, along=0.5))
[1] 2 4 3
dim(abind(x, along=1, hier.names=T)) # construct rownames in a hierarchical manner A.a, A.b, etc
[1] 8 3
dim(abind(x, along=2, hier.names=T)) # construct colnames in a hierarchical manner
[1] 4 6
abind(x, along=2, hier.names=T)
A.X A.Y A.Z B.X B.Y B.Z a 1 5 9 13 17 21 b 2 6 10 14 18 22 c 3 7 11 15 19 23 d 4 8 12 16 20 24
On 2/14/2013 3:53 AM, Rolf Turner wrote:
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 :-/
______________________________________________ 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.