[R] Embedding lists in matrices and matrices in lists
Michael Kogan
michael.kogan at gmx.net
Tue Aug 18 15:22:16 CEST 2009
Thanks, that was exactly what I looked for!
jim holtman schrieb:
> Is this what you want:
>
>
>> x <- matrix(list(),3,3) # create a matrix of lists
>> # create matrices for testing
>> for(i in 1:3){
>>
> + for (j in 1:3){
> + x[[i,j]] <- matrix(runif((i+1) * (j+1)), i+1)
> + }
> + }
>
>> x
>>
> [,1] [,2] [,3]
> [1,] Numeric,4 Numeric,6 Numeric,8
> [2,] Numeric,6 Numeric,9 Numeric,12
> [3,] Numeric,8 Numeric,12 Numeric,16
>
>> x[[2,2]] # extract one of them
>>
> [,1] [,2] [,3]
> [1,] 0.26722067 0.3823880 0.4820801
> [2,] 0.38611409 0.8696908 0.5995658
> [3,] 0.01339033 0.3403490 0.4935413
>
>> str(x) # structure of the data
>>
> List of 9
> $ : num [1:2, 1:2] 0.266 0.372 0.573 0.908
> $ : num [1:3, 1:2] 0.38 0.777 0.935 0.212 0.652 ...
> $ : num [1:4, 1:2] 0.7894 0.0233 0.4772 0.7323 0.6927 ...
> $ : num [1:2, 1:3] 0.202 0.898 0.945 0.661 0.629 ...
> $ : num [1:3, 1:3] 0.2672 0.3861 0.0134 0.3824 0.8697 ...
> $ : num [1:4, 1:3] 0.2448 0.0707 0.0995 0.3163 0.5186 ...
> $ : num [1:2, 1:4] 0.206 0.177 0.687 0.384 0.77 ...
> $ : num [1:3, 1:4] 0.186 0.827 0.668 0.794 0.108 ...
> $ : num [1:4, 1:4] 0.258 0.4785 0.7663 0.0842 0.8753 ...
> - attr(*, "dim")= int [1:2] 3 3
>
>
>
> 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