[R] Embedding lists in matrices and matrices in lists

Gabor Grothendieck ggrothendieck at gmail.com
Tue Aug 18 12:02:03 CEST 2009


Try this:

> m12 <- matrix(10, 1, 2)
> m22 <- matrix(20, 2, 2)

> # create matrix of lists of matrices
> M <- matrix(list(list(1, 2), list(m12, m12), list(t(m12)), list(m22, m22, m22)), 2, 2)
> M
     [,1]   [,2]
[1,] List,2 List,1
[2,] List,2 List,3
> M[[1,2]]
[[1]]
     [,1]
[1,]   10
[2,]   10

> M[[2,1]]
[[1]]
     [,1] [,2]
[1,]   10   10

[[2]]
     [,1] [,2]
[1,]   10   10


On Tue, Aug 18, 2009 at 4:48 AM, Michael Kogan<michael.kogan at gmx.net> wrote:
> Hi,
>
> I'm new to programming, new to R and even new to mailing lists so please be
> patient with me. I need to manage many matrices generated by an R program.
> These matrices have different dimensions and I'd like to group them somehow.
> The best way would be to have a big matrix (let's call it database) where
> every element database[x,y] consists of a list of matrices that all have the
> dimensions ncol(matrix1)=x and nrow(matrix1)=y. So those matrices have to be
> embedded into lists and the lists have to be embedded in the big database
> matrix. If I simply try
>
>   database=matrix(0,10,10)
>   database[4,4]=c(matrix1,matrix2)
>
> I get
>
>   Error in database[4, 4] = c(matrix1, matrix2) :
>     number of items to replace is not a multiple of replacement length
>   Execution halted
>
> which makes sense of course... Is there any possibility to make this work?
> Or maybe there is a better way to organize those matrices?
>
> Regards,
> Michael
>
> ______________________________________________
> 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