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? Diana
You get the number of list elements with length(D), the dimensions of M1 with dim(M1) see help with: ?dim ?length Hope this helps...>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?>Diana-- Friedrich Schuster mail at friedrich-schuster.de Tel.: +49 6221 737474 Tel.: +49 163 7374744
If they are already a matrix in the list, then you don't have to use 'as.matrix'; you can just say: M1 <- D[[1]] Now the question is, what do you mean by how do you index M1? Do you want to go through the list applying a function to each matrix? If so, then just 'lapply'. For example, to get the column means, you would do: mean.list <- lapply(D, colMeans) Can you explain in a little more detail the problem you are trying to solve. On 9/5/07, dverzi at mail.sdsu.edu <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? > > Diana > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
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