[R] list element to matrix

Jim Lemon jim at bitwrit.com.au
Thu Sep 6 11:58:28 CEST 2007


dverzi at mail.sdsu.edu wrote:
> I have created a list of "matrices" using sapply or lapply and wish to extract each of the "matrices" as a matrix.  Some of them are 2x2, 3x3, etc.
> 
> I can do this one at a time as:
> 
> M1<-as.matrix(D[[1]])
> 
> How can repeat this process for an unknown number of entries in the list?  In other words, how shall I index M1?
> 
Hi Diana,
To step through the matrices in your list (assuming that it only has one 
level):

for(mat in 1:length(D)) {
  <do what you want>
}

Jim



More information about the R-help mailing list