[R] Storing Matrices into Hash

Bert Gunter gunter.berton at gene.com
Fri Aug 1 04:56:49 CEST 2008


R also has arrays (?array). If all the matrices are the **same dimension**
and **same mode** (i.e. all numeric or all character), this is perhaps
easier, especially if you want to access various "pieces" of your matrices.

Given existing matrices, mat1, mat2,..., the slightly tricky way to combine
them into an array is:

your_array <- array(c(mat1,mat2,..),dim=c(dim(mat1,n))
## where n is the number of matrices.

This is slightly tricky because you have to understand that matrices are
actually vectors in column major order with a "dim" attribute.

An easier way to do it is to install the abind package from CRAN and just do


your_array <- abind(mat1,mat2, ..., along =3)

Accessing the matrices is then easy using array indexing.  Again, this is
only possible if they're all the same dimension. Otherwise Erik's approach
must be used.

I recommend that you spend some time learning about R's data structures. R's
internal documentation, especially An Intro to R, is a good starting point,
but there are now many books, both in English and other major European
languages, out there to help you. The CRAN website lists many of them. 

Cheers,
Bert Gunter
Genentech, Inc.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Erik Iverson
Sent: Thursday, July 31, 2008 7:01 PM
To: Gundala Viswanath
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Storing Matrices into Hash

I think a named list is probably the easiest way to start off, something 
like:

all_mat <- list(mat1 = mat1, mat2 = mat2)

all_mat$mat2



Gundala Viswanath wrote:
> Hi,
> 
> Suppose I have these two matrices (could be more).
> What I need to do is to store these matrices into a hash.
> 
> So that I can call back any of the matrix back later.
> 
> Is there a way to do it?
> 
>> mat_1
>                [,1]        [,2]
>   [1,] 9.327924e-01 0.067207616
>   [2,] 9.869321e-01 0.013067929
>   [3,] 9.892814e-01 0.010718579
>   [4,] 9.931603e-01 0.006839735
>   [5,] 9.149056e-01 0.085094444
> 
>> mat_2
>                [,1]        [,2]
>   [1,] 9.328202e-01 0.067179769
>   [2,] 9.869402e-01 0.013059827
>   [3,] 9.892886e-01 0.010711437
>   [4,] 9.931660e-01 0.006833979
>   [5,] 9.149391e-01 0.085060890
> 
> 
> This method I have is not favorable
> because it just stack the matrices together as another matrix.
> Makes it hard to get individual matrix later.
> 
> all_mat <- NULL
> all_mat <- c(all_mat, mat1,mat2)
> 
> 
> 
> - Gundala Viswanath
> Jakarta - Indonesia
> 
> ______________________________________________
> 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.

______________________________________________
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